当一个表在MySQL中为空时选择多个表 [英] Select multiple tables when one table is empty in MySQL
问题描述
我正在尝试
SELECT * FROM a, b
但是,如果其中一个表为空,它将不返回任何内容.我如何使它返回"a",即使另一个为空?
However, it doesn't return anything if one of the tables is empty. How do I make it so it returns 'a' even if the other one is empty?
推荐答案
在from
子句中使用两个表在功能上等效于cross join
:
Using two tables in the from
clause is functionally equivalent to a cross join
:
select *
from A
cross join
B
这将为B中的每一行返回A行.当B为空时,结果也为空.您可以使用left join
修复该问题.使用left join
,即使其中一个表为空,也可以返回行.例如:
This returns a row of A for every row in B. When B is empty, the result is empty too. You can fix that by using a left join
. With a left join
, you can return rows even if one of the tables is empty. For example:
select *
from A
left join
B
on 1=1
由于条件1=1
始终为true,因此它与cross join
相似,除了它也适用于空表.
As the condition 1=1
is always true, this is just like a cross join
except it also works for empty tables.
这篇关于当一个表在MySQL中为空时选择多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!