我有两个名为ticket和ticket_updates的表
在ticket_updates中有一个datetime列,而ticket.ticketnumber等于ticket_updates.ticketnumber
故障单中每行的ticket_updates中有多行
我想根据ticket_updates中的datetime列选择在故障单中添加的行数
我试过这个问题:
SELECT COUNT(*) as counter
from ticket_updates
where DATE(datetime) = '2014-11-03'
group by ticketnumber
limit 0,1
但它返回3这是错误的,因为今天在票表中添加了8行
答案 0 :(得分:1)
如果您想要总数,请删除group by
:
SELECT COUNT(*) as counter
from ticket_updates
where DATE(datetime) = '2014-11-03' ;
如果您想要每group by
的更新次数,请使用ticketnumber
。
如果您想要今天更改不同票号的数量,请使用count(distinct ticketnumber)
。
编辑:
如果您想要今天的日期并希望能够利用索引,请尝试:
where datetime >= date(now()) and datetime < date(now()) + interval 1 day
答案 1 :(得分:0)
您需要加入两个表格,如下所示:
SELECT COUNT(ticketnumber) AS counter
FROM tickets WHERE ticketnumber in (
SELECT distinct ticketnumber
FROM ticket_updates
WHERE DATE(datetime) = '2014-11-03')
答案 2 :(得分:0)
根据您的评论,这可能就是您所需要的:
SELECT count(*) FROM
(SELECT ticketnumber
FROM ticket_updates
GROUP BY ticketnumber
HAVING DATE(min(datetime)) = '2014-11-03' ) as `t1`