我有三张桌子:医院,检查和问题。医院可以进行零次或多次检查。检查可能有零个或多个问题。我需要得到一张表,其中包含所有违反医院的违规行为以及日期。表格如下:
Business Table b
----------------------------
|id|name|address|city|state|
Inspection Table i
---------------------
|id|business_id|date|
Issue Table v
-----------------------------------
|id|business_id|inspection_id|desc|
i.date desc
命令我需要的是:
Query result
--------------------------------
|b.name|b.address|i.date|v.desc|
每次检查会有多个问题,所以我需要按照上面的方式排一行。这就是我所得到的,但即使在不同的日期观察到,也会针对每个问题返回最新的检查数据。
以下是我的查询内容:
SELECT b.business_name AS Name, b.address, b.city, b.state, i.date, v.desc
FROM business_table AS b
RIGHT JOIN inspection_table i ON i.business_id = b.id
RIGHT JOIN issue_table v ON v.inspection_id = i.id
ORDER BY i.date DESC
答案 0 :(得分:0)
试试这个
SELECT b.business_name AS Name, b.address, b.city, b.state, i.date, v.desc
FROM issue_table v
LEFT JOIN inspection_table i ON i.id = v.inspection_id
LEFT JOIN business_table AS b ON v.business_id = b.id
ORDER BY v.business_id, i.date DESC