Java Questions
Java Questions

Reputation: 7953

How to combine two tables to get results

The following is my master table:

    tablename       columnname         size  order
    employee        name               25    1
    employee        sex                25    2
    employee        contactNumber      50    3
    employee        salary             25    4
    address         street             25    5
    address         country            25    6

And following is my child table:

    childid  userid  masterid  isactive  size   order
    1        1       1         Y         25     1
    2        1       2         Y         25     2
    3        1       3         N         0      0
    4        1       4         Y         50     3

I would like to get the table name columnname from master table and size,order form child table against userid when isactive is Y in child table.

Some time, if the value is not there for the particular user than get all the values like tablename, columnname, size, order where isactive isY

I am really sorry to ask this but I am not good at SQL.

Regards.

Upvotes: 0

Views: 82

Answers (1)

John Woo
John Woo

Reputation: 263683

Use INNER JOIN instead of LEFT JOIN

SELECT rcm.tablename, rcm.columnname, rcc.size, rcc.order 
from report_customise_master rcm 
        INNER JOIN report_customise_child rcc 
            ON rcm.id = rcc.masterid 
WHERE rcm.isactive = 'Y' and rcc.isactive = 'Y'

UPDATE 1

..., COALESCE(rcc.size, rcm.size) as Size, 
     COALESCE(rcc.`Order`, rcc.`order`) as `Order` 

Upvotes: 1

Related Questions