【问题标题】:Regarding mysql where clause with empty string关于带有空字符串的mysql where子句
【发布时间】:2012-09-24 20:48:57
【问题描述】:

我的查询

SELECT order_id, order_item_id, product_id, date_due, ship_via 
    FROM order_shipment 
    WHERE (product_id LIKE 'PP80403396-502%%' AND ship_via != '');

SELECT order_id, order_item_id, product_id, date_due, ship_via 
    FROM order_shipment 
    WHERE (product_id LIKE 'PP80403396-502%%' AND trim(ship_via) != '');

没有给我任何结果。

我的期望是显示 ship_via 字段为 NULL 或不为空的结果。

【问题讨论】:

  • Null 和空字符串不是一回事。也添加对 null 的检查(或使用 coalesce/isnull 同时检查两者)。
  • 您是否收到任何错误消息?
  • mysql中有coalesce吗?
  • @MichaelKrelin-hacker dev.mysql.com/doc/refman/5.0/en/…
  • 你的 where 子句应该是WHERE (product_id LIKE 'PP80403396-502%%' AND (trim(ship_via) != '' or ship_via is null));

标签: mysql sql where sql-like


【解决方案1】:

NULL 不是空字符串,也不是非空字符串。而且比较的结果既不是真的也不是假的,是NULL。您应该使用COALESCE 来测试NULL 或为空。或OR

【讨论】:

  • COALESCE 将在列表中显示第一个非空结果。我需要显示 ship_via 为空或非空字符串的结果。
  • COALESCE(ship_via!='',TRUE) ?
猜你喜欢
  • 2014-01-20
  • 1970-01-01
  • 2011-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-28
  • 1970-01-01
  • 2013-03-19
相关资源
最近更新 更多