我有一个联系人表我希望在某个条件存在时查询。我尝试了下面的查询但是出现语法错误。
SELECT *
FROM contact_details
WHERE contactDeleted` =0
AND IF ( contactVisibility = "private"
, SELECT * FROM contact_details
WHERE contactUserId = 1
, IF( contactVisibility = "group"
, SELECT * FROM contact_details
WHERE contactGroup = 3
)
)
答案 0 :(得分:1)
如果我正确理解你的问题(由于缺乏你提供的信息很困难。样本数据集和预期结果通常很有帮助),那么我认为你根本不需要IF 。以下将返回未删除且具有(visibility =“private”和userId = 1)OR(visibility =“group”和group = 3)的联系人
SELECT *
FROM contact_details
WHERE contactDeleted = 0
AND (
(contactVisibility = "public")
OR
(contactVisibility = "private" AND contactUserId = 1)
OR
(contactVisibility = "group" AND contactGroup = 3)
)
答案 1 :(得分:0)