1

试图将一个查询的结果放入WHERE另一个查询的语句中:

$query1 = "SELECT DISTINCT company_id FROM location WHERE state = 'XX'";
$result1 = mysql_query($query1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1)) {
    //Collect WHERE STATEMENT HERE
}

我尝试了以下方法:

$q1 = "SELECT company FROM company WHERE id = '16', id = '7' ORDER BY company";
$q1 = "SELECT company FROM company WHERE id = '16' AND id = '7' ORDER BY company";
$q1 = "SELECT company FROM company WHERE id = '16' && id = '7' ORDER BY company";

连同其他变体

WHERE AND如果使用不同的表名,谷歌搜索只提供了多个,但不一样。有人有任何指示吗?

4

3 回答 3

1

你也可以使用IN

$x = array('16', '7');

$q1 = "SELECT company FROM company WHERE id IN(". implode(',', $x) .") ORDER BY company";
于 2012-08-23T08:01:23.323 回答
1

对于通用代码:

$ids = array (
[0] => 2
[1] => 4
[2] => 19
[3] => 16
);  

$ids = join(',',$ids);  
$sql = "SELECT * FROM company WHERE id IN ($ids)";

参考:同样的例子

于 2012-08-23T08:12:37.487 回答
0

采用OR

$q1 = "SELECT company FROM company WHERE (id = '16' OR id = '7') ORDER BY company";
于 2012-08-23T07:59:36.090 回答