我需要一些帮助才能将这组查询转换为单个查询,有人可以帮助我吗?
$occurrences = mysqli_query($link,"SELECT * FROM occurrences WHERE close_id_user order by close_date desc limit 10");
while ($row = mysqli_fetch_assoc($occurrences)){
$computers = mysqli_query($link,"SELECT * FROM computers id='".$row['id_computer']."'");
$row_computers = mysqli_fetch_assoc($computers);
$manufacturers = mysqli_query($link,"SELECT * FROM manufacturers WHERE id='".$row_computers['id_manufacturer']."'");
$row_manufacturers = mysqli_fetch_assoc($manufacturers);
$clients = mysqli_query($link,"SELECT * FROM clients WHERE id='".$row_computers['id_client']."'");
$row_clients = mysqli_fetch_assoc($clients);
}
答案 0 :(得分:0)
很难确定在你的问题中结合4的最佳查询是什么。困难在于您不包括如何组合它们。我们可以看到每个表之间的关系基于computer_id,client_id和manufacturer_id,但是很难分辨出while
循环的影响以及它如何转换为Join
。
同样,以下内容应该让你接近:
SELECT *
FROM occurences
LEFT OUTER JOIN computers ON occurences.id_computer = computers.id
LEFT OUTER JOIN manufacturers ON occurences.id_manufacturers = manufacturers.id
LEFT OUTER JOIN clients ON occurences.id_client = clients.id
WHERE close_id_user
这里我们使用LEFT OUTER JOIN
来引入每个表。这可以确保您将获得occurences
的所有记录,无论其他三个表中的任何一个是否与其各自的id
字段具有匹配值。
查看系统中的结果,然后从那里调整查询,将结果集转换为适合您的格式。