Mysql WHERE子句中的ISNULL(值,0)
我有这样的要求:Mysql WHERE子句中的ISNULL(值,0),mysql,where,isnull,Mysql,Where,Isnull,我有这样的要求: SELECT sc.no, scl.quantite, scl.description, scl.poids, scl.prix, sl_ref.refsl, sl_ref.codetva, sl_ref.tauxtva, sl_ref.compte FROM shop_commande AS sc, shop_commande_ligne AS scl, selectline_ref AS sl_ref WHERE sc.id = scl.shop_commande_i
SELECT sc.no, scl.quantite, scl.description, scl.poids, scl.prix, sl_ref.refsl, sl_ref.codetva, sl_ref.tauxtva, sl_ref.compte
FROM shop_commande
AS sc, shop_commande_ligne AS scl, selectline_ref AS sl_ref
WHERE sc.id = scl.shop_commande_id
AND sl_ref.refshop = ISNULL(scl.shop_article_id, 0)
AND sc.id NOT IN (SELECT id_command FROM selectline_flag)
有时,在sl_shop_article_id中,有一个空值。我想用0替换它,因此该子句:
sl_ref.refshop = scl.shop_article_id
即使scl.shop\u article\u id为空,也可以工作。为此,我尝试使用ISNULL函数,但它使请求出错,因此出现错误:
1582-对本机函数“ISNULL”的调用中的参数计数不正确
如何使用它?我相信您正在尝试使用
IFNULL()
函数。如果将ISNULL
替换为IFNULL
,则应修复您的查询
SELECT
sc.no
, scl.quantite
, scl.description
, scl.poids
, scl.prix
, sl_ref.refsl
, sl_ref.codetva
, sl_ref.tauxtva
, sl_ref.compte
FROM shop_commande
AS sc, shop_commande_ligne AS scl, selectline_ref AS sl_ref
WHERE sc.id = scl.shop_commande_id
AND sl_ref.refshop = IFNULL(scl.shop_article_id, 0)
AND sc.id NOT IN (SELECT id_command FROM selectline_flag)
我建议您进一步使用COALESCE()
而不是IFNULL()
,因为COALESCE()
是SQL标准的一部分(而IFNULL()
不是)
您可以在这里看到
ISNULL()
和IFNULL()
函数之间的区别,感谢您的链接和快速回答!正如Ike所说,COALESCE是SQL标准的一部分,我将使用他的答案,但您的答案也很好!工作真的很好,现在没有关于合并,所以谢谢!
SELECT sc.no, scl.quantite, scl.description, scl.poids,
scl.prix, sl_ref.refsl, sl_ref.codetva, sl_ref.tauxtva, sl_ref.compte
FROM shop_commande
AS sc, shop_commande_ligne AS scl, selectline_ref AS sl_ref
WHERE sc.id = scl.shop_commande_id
AND sl_ref.refshop = COALESCE(scl.shop_article_id, 0)
AND sc.id NOT IN (SELECT id_command FROM selectline_flag)