编程语言
首页 > 编程语言> > php-编写查询以获取数组并在子查询中使用该数组

php-编写查询以获取数组并在子查询中使用该数组

作者:互联网

我想做的是从第一个查询中获取结果,并将结果传递到数组中,然后在子查询中使用它们.如果我将ID手动输入到子查询中,则这两个查询将分别工作.有没有办法链接这两个查询?

我已经使用了这段代码

$result = mysql_query("SELECT v2.video_id as v2id FROM VideoTags AS v1 JOIN VideoTags AS v2 USING ( tag_id ) WHERE v1.video_id =1 AND v1.video_id <> v2.video_id GROUP BY v2.video_id ORDER BY COUNT( * ) DESC"); 

$values = array();

while ($row = mysql_fetch_array( $result )) {
  $values[] = $row['v2id'];
}

echo join(", ", $values);

$resultone = mysql_query("SELECT * FROM videos WHERE video_id IN (' . join(',', $values). ')"); 

while ($row = mysql_fetch_array( $resultone )) {
  echo "name ".$row['video_name'];
}

谢谢你的帮助.

解决方法:

是的,它称为子查询(您使用的不是子查询,因为它在另一个查询中不包含一个查询.

SELECT * 
FROM videos 
WHERE video_id IN (
    SELECT v2.video_id 
    FROM VideoTags AS v1 
    JOIN VideoTags AS v2 USING ( tag_id ) 
    WHERE v1.video_id =1 AND v1.video_id <> v2.video_id 
    GROUP BY v2.video_id ORDER BY COUNT( * ) DESC
)

标签:subquery,arrays,mysql,php
来源: https://codeday.me/bug/20191208/2090572.html