Sql加入两个表并显示具有空值的表

时间:2017-04-05 06:47:43

标签: mysql sql select join null

我有两个表,我已经做了内连接,我想显示那些有价格,sale_price,stock_unit字段为空的行,或者我没有插入那些价格,sale_price,stock_unit。

$live_query = "select shower.id,  shower.name, shower.firstimage,shower_pricing.price,  shower_pricing.list_id, shower_pricing.sale_price,shower_pricing.discount,shower_pricing.stock_unit
from shower
inner join shower_pricing
on shower.id=shower_pricing.id 
where shower_pricing.price,shower_pricing.sale_price,shower_pricing.stock_unit is null";

谢谢。

5 个答案:

答案 0 :(得分:2)

SQL NULL特别,你必须做WHERE field IS NULL,因为NULL不能等于任何东西,包括它自己(即:NULL = NULL总是假的)。

答案 1 :(得分:2)

听起来你需要左外连接而不是内连接。

select 
    shower.id, shower.name, shower.firstimage,
    shower_pricing.price, shower_pricing.list_id, 
    shower_pricing.sale_price,shower_pricing.discount,
    shower_pricing.stock_unit
from shower left outer join shower_pricing 
    on shower.id=shower_pricing.id 
where shower_pricing.price is null 
    or shower_pricing.sale_price is null 
    or shower_pricing.stock_unit is null

答案 2 :(得分:1)

查询淋浴中所有那些没有相同于shower_pricing的记录,并查询那些在shower_pricing中有条目的记录但是给定的属性,即price,sale_price,stock_unit字段是两种不同的情况。

在shower_pricing中没有条目的项目

$live_query = "select shower.id,  shower.name, shower.firstimage,shower_pricing.price,  
shower_pricing.list_id, shower_pricing.sale_price,shower_pricing.discount,
shower_pricing.stock_unit
from shower
left join shower_pricing
on shower.id=shower_pricing.id 
where shower_pricing.id is null;

具有条目但给定属性为null的项目

$live_query = "select shower.id,  shower.name, shower.firstimage,shower_pricing.price,  
shower_pricing.list_id, shower_pricing.sale_price,shower_pricing.discount,
shower_pricing.stock_unit
from shower
inner join shower_pricing
on shower.id=shower_pricing.id 
where shower_pricing.price is null OR shower_pricing.sale_price is null 
OR shower_pricing.stock_unit is null;

答案 3 :(得分:0)

如果你想要所有这些都是NULL,那么Query应该看起来像这样,

$live_query = "select shower.id,  shower.name, shower.firstimage,shower_pricing.price,  shower_pricing.list_id, shower_pricing.sale_price,shower_pricing.discount,shower_pricing.stock_unit
from shower
inner join shower_pricing
on shower.id=shower_pricing.id 
where shower_pricing.price IS NULL AND shower_pricing.sale_price IS NULL AND shower_pricing.stock_unit IS NULL";

如果您希望任何值为null,则查询将如下所示

$live_query = "select shower.id,  shower.name, shower.firstimage,shower_pricing.price,  shower_pricing.list_id, shower_pricing.sale_price,shower_pricing.discount,shower_pricing.stock_unit
from shower
inner join shower_pricing
on shower.id=shower_pricing.id 
where shower_pricing.price IS NULL OR shower_pricing.sale_price IS NULL OR shower_pricing.stock_unit IS NULL";

答案 4 :(得分:0)

试试这个:

SELECT s.id, s.name, s.firstimage, sp.price, sp.list_id, sp.sale_price, sp.discount, sp.stock_unit
FROM shower s 
INNER JOIN shower_pricing sp ON s.id = sp.id 
WHERE (sp.price IS NULL OR sp.price = '') 
  AND (sp.sale_price IS NULL OR sp.sale_price = '') 
  AND (sp.stock_unit IS NULL OR sp.stock_unit = '')