我有两个相关的表;类别和订阅。类别->订阅之间的关系并不总是存在。所以我需要这样的东西:

SELECT * FROM categories
LEFT JOIN subscriptions
ON subscriptions.category_id = categories.category_id
WHERE
 IF EXISTS(subscriptions.is_enable)
  categories.type = 'premium' AND
  subscriptions.is_enable = 1
ELSE
  categories.type = 'premium' AND


有什么办法可以写这样的条件?

最佳答案

您可以像这样在逻辑上重写条件:

SELECT * FROM categories
LEFT JOIN subscriptions
ON subscriptions.category_id = categories.category_id
WHERE (subscriptions.is_enable IS NULL OR subscriptions.is_enable = 1)
  AND categories.type = 'premium'

关于mysql - 在WHERE条件下的IF语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50398580/

10-11 01:55