1
客戶可以進行多種查詢,並且查詢可以有許多更新。我正在嘗試查看每個查詢和最新更新。我能夠提取最新的日期,但不能更新與之相關的更新。我看到類似於我的答案,但他們似乎只處理一個連接,我不明白我如何使用這些來找到解決方案。最大日期組3表格
這裏是我的SQL
select c.name, i.inquirycontent, u.updatecontent, max(u.date) from inquiries i
inner join customers c on c.customerid = i.customerid
left join updates u on u.inquiryid = i.inquiryid
group by i.inquiryid
即使我省略了客戶的表,我仍然無法匹配的最新更新內容。我怎樣才能做到這一點?
編輯 - 的要求,這裏是一些樣本數據
+------------+------+
| customerid | name |
+------------+------+
| 1 | jeff |
+------------+------+
| 2 | anne |
+------------+------+
+-----------+-----------------+------------+
| inquiryid | inquirycontent | customerid |
+-----------+-----------------+------------+
| 1 | inquiry1content | 1 |
+-----------+-----------------+------------+
| 2 | inquiry2content | 1 |
+-----------+-----------------+------------+
| 3 | inquiry3content | 2 |
+-----------+-----------------+------------+
+----------+-----------------+-----------+----------+
| updateid | updatecontent | inquiryid | date |
+----------+-----------------+-----------+----------+
| 1 | update1content | 1 | 01-01-17 |
+----------+-----------------+-----------+----------+
| 2 | update2content | 1 | 03-01-17 |
+----------+-----------------+-----------+----------+
| 3 | update3content | 3 | 04-01-17 |
+----------+-----------------+-----------+----------+
這裏就是我希望查詢輸出 -
+------+-----------------+----------------+----------+
| name | inquirycontent | latestupdate | date |
+------+-----------------+----------------+----------+
| jeff | inquiry1content | update2content | 03-01-17 |
+------+-----------------+----------------+----------+
| jeff | inquiry2content | NULL | NULL |
+------+-----------------+----------------+----------+
| anne | inquiry3content | update3content | 04-01-17 |
+------+-----------------+----------------+----------+
請發表您的表的幾行 – RealCheeseLord
如何格式化? –
縮進4個空格,並嘗試使它看起來不錯,如代碼塊 – RealCheeseLord