这是我的表(多对多的关系)。 他们的目标 - 为不同的请求保留状态变化的历史记录。 (我从插图中删除了对问题没有意义的所有字段)
1的请
id (primary, ai) | name (varchar) | phone (varchar) | text (varchar)
1 | Maria | 9232342323 | "fist text"
2 | Petr | 2342342323 | "second text"
3 | Lenny | 2342342323 | "third text"
2的以下状态
id (primary, ai) | title (varchar)
1 | New
2 | In progress
3 | Abort
3的 Requests_has_Statuses
id (primary, ai) | requests_id (fk) | statuses_id (fk) | comment (varchar) | date (timestamp)
1 | 1 | 1 | "Fist comment about a Maria's request" | 2014-08-08 12:24
2 | 1 | 2 | "Second comment about Maria" | 2014-08-08 12:26
3 | 1 | 2 | "Third comment about Maria" | 2014-08-08 12: 57
4 | 2 | 1 | "First comment about Petr" | 2014-08-08 13:23
5 | 3 | 1 | "First comment about Lenny" | 2014-08-08 13:45
6 | 2 | 3 | "Second comment about Petr" | 2014-08-08 14:00
我的目标是选择这样的输出:
1 | Maria | 9232342323 | "In progress" | "Third comment about Maria" | 2014-08-08 12: 57
2 | Petr | 2342342323 | "Abort" | "Second comment about Petr" | 2014-08-08 14:00
3 | Lenny | 2342342323 | "New" | "First comment about Lenny" | 2014-08-08 13:45
简单来说,我需要输出当前的实际状态和每个请求的信息。
我试图这样做:
SELECT r.name, r.phone, st.title, rhs.comment, rhs.date
FROM requests AS r
INNER JOIN Requests_has_Statuses AS rhs ON rhs.requests_id = r.id
INNER JOIN Statuses AS st ON rhs.statuses_id = st.id;
但我有一个不必要的重复,如:
1 | Maria | 9232342323 | "New" | "Fist comment about a Maria's request" | 2014-08-08 12:24
1 | Maria | 9232342323 | "In progress" | "Second comment about Maria" | 2014-08-08 12: 26
1 | Maria | 9232342323 | "In progress" | "Third comment about Maria" | 2014-08-08 12: 57
2 | Petr | 2342342323 | "New" | "First comment about Petr" |2014-08-08 13:23
3 | Lenny | 2342342323 | "New" | "First comment about Lenny" | 2014-08-08 13:45
2 | Petr | 2342342323 | "Abort" | "Second comment about Petr" | 2014-08-08 14:00
你能帮我提一些建议或解决方案吗? 感谢
答案 0 :(得分:0)
添加过滤器以仅获取每个请求者的最新状态:
SELECT r.name, r.phone, st.title, rhs.comment, rhs.date
FROM requests AS r
INNER JOIN Requests_has_Statuses AS rhs ON rhs.requests_id = r.id
INNER JOIN Statuses AS st ON rhs.statuses_id = st.id
where rhs.id in
(select max(id)
from Requests_has_Statuses AS rhs2
where rhs.requests_id = rhs2.requests_id
group by rhs2.requests_id
)
答案 1 :(得分:0)
另外请尝试下一个查询,使用DISTINCT(只返回一行),最后按日期排序(ORDER BY):
SELECT DISTINCT r.name, r.phone, st.title, rhs.comment, rhs.date
FROM requests AS r
INNER JOIN Requests_has_Statuses AS rhs ON rhs.requests_id = r.id
INNER JOIN Statuses AS st ON rhs.statuses_id = st.id
ORDER BY rhs.date DESC;
答案 2 :(得分:0)
在编写交叉表计算时,我从不使用JOIN。 试试这个,
SELECT name, phone, title, comment, date , requests_id , title
FROM Requests Statuses Requests_has_Statuses
Where Requests.id=Statuses.id AND Requests.id=Requests_has_Statuses.id
<强> 修改 强>
GROUP BY name;
答案 3 :(得分:0)
试试这个:
SELECT r.id, r.phone, st.title, rhs.comment, rhs.date
FROM (
SELECT r.id, max(rhs.date) AS date
FROM Requests_has_Statuses AS rhs
INNER JOIN Requests r ON r.id = rhs.requestId
GROUP BY r.id) AS temp
INNER JOIN Requests AS r ON temp.id = r.id
INNER JOIN Requests_has_Statuses AS rhs ON rhs.requestId = temp.id and rhs.date = temp.date
INNER JOIN Statuses AS st ON rhs.statusId = st.id