查询多个表,但在没有 JOIN mysql 的情况下从任何一个表中获取结果
query multiple tables but get results from any one of the tables without JOIN mysql
我的 2 table 的描述:
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
我希望输出仅来自 br 的 1 行,其中 bid = 2174
我想同时查询出价的 table 并获取匹配的行。
一个特定的出价可以在任何一个 table 中,不能同时存在于两个 table 中。
SELECT bt.*, br.* FROM bt, br where br.bid = 2174 OR bid = 2174
但这给了我来自一个 table 的匹配行 + 来自其他 table 的所有行。
请提出建议。
因为我试图在 mysql 中执行此操作,所以也标记 mysql
我猜 union all
会做你想做的事:
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;
我的 2 table 的描述:
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
我希望输出仅来自 br 的 1 行,其中 bid = 2174
我想同时查询出价的 table 并获取匹配的行。 一个特定的出价可以在任何一个 table 中,不能同时存在于两个 table 中。
SELECT bt.*, br.* FROM bt, br where br.bid = 2174 OR bid = 2174
但这给了我来自一个 table 的匹配行 + 来自其他 table 的所有行。 请提出建议。
因为我试图在 mysql 中执行此操作,所以也标记 mysql
我猜 union all
会做你想做的事:
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;