id title
--------------------------
1 mysql websites
2 html tips
3 mysql tricks
4 javascript tutorial
5 mysql queries
6 javascript framework
我想在一个查询中为多行选择相关文章,并按 id 对它们进行分组。那可能吗?
就像是
SELECT
id, title
FROM
articles
WHERE
title LIKE (TITLE_OF_CURRENT_ROW)
AND id IN (1,4)
GROUP BY
id;
结果是
大批 ( // id = 1 的结果 0 => 数组 ( 0 => 数组(id => 1,标题 => 'mysql 网站'), 1 => 数组(id => 3,标题 => 'mysql 技巧'), 2 => 数组(id => 5,标题 => 'mysql 查询') ), // id = 4 的结果 1 => 数组 ( 0 => 数组(id => 4,标题 => 'javascript 教程'), 1 => 数组(id => 6,标题 => 'javascript 框架') } }
编辑:
我这样做的原因是因为我想 void queries inside loop
。例如,我正在生成 100 行并将它们保存到文件中。
$result = $mysql->query("SELECT id, title FROM articles LIMIT 100");
while($row = $result->fetch_assoc()) {
$related_result = $mysql->query("SELECT id, title FROM articles WHERE title LIKE '%".$row['title']."%' AND id != ".$row['id']);
// while... get related data
save_data_to_file($row['id'], $row['title'], $related['ids'], $related['titles']);
}
上面的相关查询会重复 100 次,我该怎么走呢?