Search code examples
mysqlsqldatabaserdbms

query multiple tables but get results from any one of the tables without JOIN mysql


description of my 2 tables:

table1
id (int),
bid (int),
trs (varchar 10)

table2
id (int),
bid (int),
ref_table (varchar 10)
trs (varchar 10)

br 
id    bid     trs
1     213     1913
2     2174    1920

bt 
id    bid     ref_table   trs 
1     212     room        1913 
2     214     room        1920

i want the output as only 1 row from br where bid = 2174

I want to query both the tables on bid and get the matching rows. a particular bid can be in any one table, cant exists in both tables.

SELECT bt.*, br.* FROM bt, br where br.bid = 2174 OR bid = 2174

but this give me matching rows from one table + all the rows from other table. please suggest.

since i am trying to do this in mysql, tagging mysql also


Solution

  • I am guessing that union all does what you want:

    select id, bid, trs, null as ref_table
    from br
    where bid = 2174
    union all
    select id, bid, trs, ref_table
    from bt
    where bid = 2174;