使用IFNULL选择Mysql

时间:2014-11-20 16:36:31

标签: mysql

我正在尝试使用IFNULL来确定在where子句中使用列,但我不相信我正确地做了。有人有什么建议吗?

SELECT * FROM bdesclookuptable 
WHERE LOWER('Santa Clause Home Address') LIKE CONCAT('%',LOWER(lText),'%') 
AND  LOWER('Santa Clause Home Address') LIKE CONCAT('%',LOWER(IFNULL(plusand,'ilikethispaintingoverhead')),'%') 
AND  LOWER('Santa Clause Home Address') NOT LIKE CONCAT('%',LOWER(IFNULL(andnot,'ilikethispaintingoverhead')),'%');

table 
id  lText          bbid ttype   andnot     plusand
16  Home Address    11  13      null        Santa
17  Home Address    11  13      Work        Santa
18  Home Address    12  15     Mrs Clause   null

我希望此查询仅提取ID为16的记录。

1 个答案:

答案 0 :(得分:1)

你有3个条件。为了成功,他们都必须得到满足。

条件1符合所有三条记录,因为Home Address中包含Santa Clause Home Address

条件2满足16和17. 18失败,因为plusand被替换为模式中未包含的长词。

条件3再次满足所有三个条件。

所以你应该得到16和17。

为了避免17,你应该使其中一个条件失败,例如: G。字符串中包含andnot值以使其失败。

相关问题