我有这个SQL查询:
SELECT
*,
COUNT(*) as count
from tickets
where
status = :status
and DATE(closed_timestamp) = '".date("Y-m-d")."'
group by assigned_to
order by count(*) DESC
LIMIT 3
但我想从closed_timestamp
表中删除tickets
列,并根据datetime
表格中的ticket_updates
列运行查询
ticketnumber
表中的 tickets
等于ticketnumber
表中的ticket_updates
(多行)
因此它应该在没有closed_timestamp
列的情况下运行上述查询,并将其基于datetime
表中的最新ticket_updates
值
示例
在故障单表格中有两行ticketnumber = 1234
和5678
ticket_updates
表中有3行..两行有ticketnumber = 1234
,另一行有ticketnumber = 5678
我想显示票证中的所有行status = 'Completed'
和上次更新(ticket_updates
表)等于当前日期(2014-05-19)
答案 0 :(得分:0)
使用join
查询,如下所示,
SELECT
t.*,
COUNT(t.*) as count
from tickets as t
LEFT JOIN ticket_updates as tu
ON tu.ticketnumber = t.ticketnumber
where
t.status = :status
group by t.assigned_to
order by count(t.*) DESC
LIMIT 3
答案 1 :(得分:0)
试试这个:
SELECT *
FROM tickets t
JOIN (
SELECT ticketnumber
FROM ticket_updates
WHERE DATE(datetime) = CURDATE()
) u
ON t.ticketnumber = u.ticketnumber
WHERE status = 'Completed'