我正在尝试在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时,该语句不需要包含最大值时,我应该如何写条件

最佳答案

    $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';

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

10-17 03:09