MySQL where where条款以显示特定年龄

时间:2013-01-03 03:53:35

标签: php mysql conditional-statements

所有Mysql查询似乎都能完美地满足“年龄”的需求。从我的桌子。例如,我需要年龄来将此条件从age >=20过滤到age <=30,外翻似乎有效,但不是年龄。

例如,点击此处的链接a link

$sql = "SELECT * FROM  `file_records` 
           WHERE  (`country` = '$country' OR '$country' IS NULL) 
               OR (`gender` = '$gender' OR'$gender' IS NULL) 
               OR (`cast` = '$cast' OR '$cast' IS NULL) 
               OR (
                   (`age` >= '$age_from' OR   '$age_from' IS NULL) 
                     AND (`age` <= '$age_to' OR '$age_to' IS NULL)
                  ) 
           LIMIT 0 , 30";

4 个答案:

答案 0 :(得分:0)

您可以使用像

这样的mysql BETWEEN函数
age BETWEEN 20 AND 30

答案 1 :(得分:0)

您正在检查&gt; =来自和迄今为止..试试这个..

$sql = "SELECT * FROM  `file_records` 
         WHERE (`country` = '$country' OR '$country' IS NULL) 
           OR (`gender` = '$gender' OR' $gender' IS NULL) 
           OR (`cast` = '$cast' OR '$cast' IS NULL) 
           OR ((`age` >= '$age_from' OR '$age_from' IS NULL) 
                AND (`age` <= '$age_to' OR '$age_to' IS NULL)) 
         LIMIT 0 , 30";

答案 2 :(得分:0)

您可以选择和年龄相似的运营商

SELECT column_name(s)
FROM table_name
WHERE age
BETWEEN age >=20 and age <=30

答案 3 :(得分:0)

如果你echo $sql;,你会发现它是不可能的    '' IS NULL是真的。

你想要像

这样的东西
OR ((`age` >= '$age_from' OR '$age_from' = '') 
            AND (`age` <= '$age_to' OR '$age_to' = ''))