【问题标题】:Sql Join two table and show the table which has null valuesSql 连接两个表并显示具有空值的表
【发布时间】:2017-08-30 15:08:16
【问题描述】:

我有两个表,我已经完成了内部连接,我想显示那些价格、销售价格、库存单位字段为空的行,或者我没有插入这些价格、销售价格、库存单位。

$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";

谢谢。

【问题讨论】:

标签: mysql sql select join null


【解决方案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;

【讨论】:

  • 没有问题 :) 当您期望来自两个表的结果时,您使用内连接,因此如果您想查找表 A 中存在但表 B 中不存在的结果,那么内连接将不会最佳选择。在这种情况下,您可以使用左连接,左连接为您提供表 A 的所有结果,并且只提供与表 A 的条目相对应的表 B 的结果。
【解决方案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

【讨论】:

    【解决方案3】:

    试试这个:

    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 = '') 
    

    【讨论】:

      【解决方案4】:

      如果你希望它们都为 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";
      

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

      $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";
      

      【讨论】:

      • 现在它正在工作我使用内连接而不是左外连接。
      • 选择 Shower.id、shower.name、shower.firstimage、shower_pricing.price、shower_pricing.list_id、shower_pricing.sale_price、shower_pricing.discount、shower_pricing.stock_unit 从淋浴器左侧外部加入 Shower_pricing on Shower.id =shower_pricing.id 其中 Shower_pricing.price 为 null 或 Shower_pricing.sale_price 为 null 或 Shower_pricing.stock_unit 为 null
      【解决方案5】:

      SQL NULL 的特殊性,你必须这样做WHERE field IS NULL,因为 NULL 不能等于任何东西,包括它自己(即:NULL = NULL 始终为 false)。

      【讨论】:

      • 试试:where shower_pricing.price and shower_pricing.sale_price and shower_pricing.stock_unit IS NULL";
      猜你喜欢
      • 2015-07-13
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多