一个mysql查询中的多个where子句

时间:2016-05-23 18:01:51

标签: php mysql sql select

我希望通过一个查询获得这些查询提供的所有结果;

1- $query = mysql_query("select * from table where view='1'");
2- $query = mysql_query("select * from table where view='2'");
3- $query = mysql_query("select * from table where view='3'");
4- $query = mysql_query("select * from table where view='4'");
.
.
.
999999- $query = mysql_query("select * from table where view='999999'");

我怎样才能轻松完成?

3 个答案:

答案 0 :(得分:6)

有几种方法。对于这种极端情况(或任何范围),请使用BETWEEN

SELECT * FROM table WHERE view BETWEEN 1 AND 999999

如果不是范围,请使用IN

SELECT * FROM table WHERE view IN (1,2,3,4,999999)

答案 1 :(得分:3)

您可以使用一对>=<=运算符:

$query = 
mysql_query("select * from table where view >= '1' AND view <= '999999'");

答案 2 :(得分:2)

如果是序列号

$query = mysql_query("select * from table where view between 1 and 999999");

如果它是离散的数字

$query = mysql_query("select * from table where view in(1, 2, 3, 10, 16, 88)");

也不建议使用mysql_query来自php 7的removed代替pdomysqli使用preapre防止SQL注入

这是pdo的例子

$query = $pdo->prepare("select * from table where view between ? and ?");
$query->execute([1,99999]);