我正在尝试在我的mysql查询中写一个条件:
SELECT g.* FROM groups AS g
JOIN users AS u
ON u.user_posts >= g.post_min
AND u.user_posts <= g.post_max
AND u.points >= g.point_min
AND u.points <= g.point_max
AND u.uid = "'.$uid.'"
当g.post_max或g.point_max等于0时,如何编写条件语句不需要包含最大值
答案 0 :(得分:1)
$sql = 'SELECT g.id,g.point_amount
FROM groups AS g
JOIN users AS u
ON u.user_posts >= g.post_min
AND (u.user_posts <= g.post_max OR (g.post_max=0 OR g.point_max=0))
AND u.points >= g.point_min
AND (u.points <= g.point_max OR (g.post_max=0 OR g.point_max=0))
AND u.uid = "'.$uid.'"
LIMIT 1';
答案 1 :(得分:0)
用OR加入他们;要么跳过条件为真,要么不能跳过术语:
(g.post_max = 0) OR (u.user_posts <= g.post_max)
(g.point_max = 0) OR (u.points <= g.point_max)
编辑:将其插入您的查询:
$sql = 'SELECT g.id,g.point_amount
FROM groups AS g
JOIN users AS u
ON u.user_posts >= g.post_min
AND u.points >= g.point_min
AND ((g.post_max = 0) OR (u.user_posts <= g.post_max))
AND ((g.point_max = 0) OR (u.points <= g.point_max))
AND u.uid = "'.$uid.'"
LIMIT 1';
再次编辑:我在阅读时失败了;误解了你的问题。这应该是它的样子。