PHP& MySQL查询问题

时间:2010-10-12 17:27:04

标签: php mysql

如何在MySQL查询中检查每个数组中的每个值。我希望这是有道理的。

SELECT articles_comments.comment_id FROM articles_comments
WHERE articles_comments.comment_id =

Array
(
    [0] => 5
    [1] => 6
    [2] => 10
    [3] => 11
    [4] => 12
    [5] => 17
    [6] => 3
    [7] => 4
    [8] => 7
    [9] => 8
    [10] => 9
    [11] => 16
)

AND articles_comments.user_id = 

Array
(
    [2] => 4
    [3] => 4
    [4] => 4
    [8] => 4
    [9] => 4
    [10] => 4
)

3 个答案:

答案 0 :(得分:4)

就像那样:

$query = "SELECT articles_comments.comment_id FROM articles_comments
WHERE articles_comments.comment_id IN(" . implode(",", $commentArray) . ")
  AND articles_comments.user_id IN(". implode(",", $userArray) . ")";

答案 1 :(得分:0)

SELECT articles_comments.comment_id 
FROM articles_comments
WHERE articles_comments.comment_id in (1,2,3,4,5)
AND articles_comments.user_id in (6,7,8,9)

答案 2 :(得分:0)

尝试

<?php  

$arr1 = Array(5,6,10,11,12,17,3,4,7,8,9,16);

$arr2 = Array(4,4,4,4,4,4);


$sql = "SELECT articles_comments.comment_id FROM articles_comments
WHERE articles_comments.comment_id IN ("
.implode(",",$arr1).
")
AND articles_comments.user_id IN ("
.implode(",",$arr2).
") ";

echo $sql;

// You still need to connect to a database, execute this query and display the results
// This is just how you would parse the array into the query

?>

它为SQL提供了这个:

SELECT articles_comments.comment_id FROM articles_comments WHERE
articles_comments.comment_id IN (5,6,10,11,12,17,3,4,7,8,9,16) AND
articles_comments.user_id IN (4,4,4,4,4,4)