当一个表在MySQL中为空时选择多个表 [英] Select multiple tables when one table is empty in MySQL

查看:45
本文介绍了当一个表在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆