>有比这更好的方法吗?
SELECT * FROM tagcloud
WHERE (usertag_tag = 1
OR usertag_tag = 2
OR usertag_tag = 3
OR usertag_tag = 4)
如果我想向查询添加更多标签,是否继续添加 OR 语句?
您可以使用
简单版本
SELECT * FROM tagcloud
WHERE usertag_tag in (1,2,3,4);
希望这有帮助
您可以使用 IN 语句:
SELECT * FROM tagcloud WHERE user_tag IN(1,2,3,4)
类似的东西 从标签云中选择 * 其中usertag_tag (1, 2, 3, 4)。
您应该准备一个列表并从中进行选择:
$tags = array( 1, 2, 3, 4 );
$query = "SELECT * FROM tagcloud WHERE usertag_tag IN (" . implode( ',', $tags ) . ")";
IN,
但我想您插入的 id 是动态的(可以来自另一个表)所以你可以使用
SELECT * FROM tagcloud
WHERE usertag_tag in (select id from the_other_table)
如果没有,那没关系
SELECT * FROM tagcloud
WHERE usertag_tag in (1,2,3,4)
你可以使用这样的数组,
$sql = "SELECT * FROM tagcloud WHERE user_tag IN ";
$j = 6;
for($i = 0; $i< $j; $i++){
$queryArray[] .= "('". $i. "')";
}
$sql .= implode(',', $queryArray);
只需将j
更改为所需的值即可。
在 中使用 MySQL
SELECT * FROM tagcloud
WHERE (usertag_tag = 1
OR usertag_tag = 2
OR usertag_tag = 3
OR usertag_tag = 4)
/* You are checking whether usertag_tag is either 1, 2, 3 OR 4*/
相当于:
SELECT * FROM tagcloud
WHERE (usertag_tag IN (1, 2, 3, 4))
/* You are checking usertag_tag is one of the values in the array given as
array(1, 2, 3, 4)
If you want to add more values, just add elements to the array, that is it.
*/
解释:
如果我们比较单个值,我们使用 =
.
如果我们需要在其中一个值(值数组)中找到具有给定字段的行,我们使用 IN
.
MySQL IN
在逻辑上与 PHP 的 in_array() 相同