0

我的桌子:

tbl_questions =>
qid
question
detail
mcid
cid
uid
answercount
dateposted
status
showname
emailnotify
question_type_id

我想按小时分组所有行。{dateposted = Ymd H:i:s} 我想将 {question_type_id 更新为 2} 在那一小时内我有更多行。

好吧,如果有人每小时有 15 个问题,我想给他们类型 2,但只有那个小时内的那些消息(行)。对不起,很难说出我到底想要什么,我的英语很差。

sample data tbl_questions =>

qid = 1, uid = 1, dateposted = 2013-01-01 10:11:10, question_type_id = 1
qid = 2, uid = 1, dateposted = 2013-01-01 10:16:10, question_type_id = 1
qid = 3, uid = 1, dateposted = 2013-01-01 10:18:10, question_type_id = 1
qid = 4, uid = 1, dateposted = 2013-01-01 10:19:10, question_type_id = 1
qid = 5, uid = 1, dateposted = 2013-01-01 10:20:10, question_type_id = 1
qid = 6, uid = 1, dateposted = 2013-01-01 10:22:10, question_type_id = 1
qid = 7, uid = 1, dateposted = 2013-01-01 10:23:10, question_type_id = 1
qid = 8, uid = 1, dateposted = 2013-01-01 10:24:10, question_type_id = 1
qid = 9, uid = 1, dateposted = 2013-01-01 10:25:10, question_type_id = 1
qid = 10, uid = 1, dateposted = 2013-01-01 10:26:10, question_type_id = 1
qid = 11, uid = 1, dateposted = 2013-01-01 10:27:10, question_type_id = 1
qid = 12, uid = 2, dateposted = 2013-01-01 10:17:10, question_type_id = 1
qid = 13, uid = 2, dateposted = 2013-01-01 10:27:10, question_type_id = 1
qid = 14, uid = 1, dateposted = 2013-01-01 12:27:10, question_type_id = 1

我想要update他们,rows因为posted by uid = 1一小时内超过限制。我需要set question_type_id = 2在那些行中。

所以预期的输出是: tbl_questions =>

qid = 1, uid = 1, dateposted = 2013-01-01 10:11:10, question_type_id = 2
qid = 2, uid = 1, dateposted = 2013-01-01 10:16:10, question_type_id = 2
qid = 3, uid = 1, dateposted = 2013-01-01 10:18:10, question_type_id = 2
qid = 4, uid = 1, dateposted = 2013-01-01 10:19:10, question_type_id = 2
qid = 5, uid = 1, dateposted = 2013-01-01 10:20:10, question_type_id = 2
qid = 6, uid = 1, dateposted = 2013-01-01 10:22:10, question_type_id = 2
qid = 7, uid = 1, dateposted = 2013-01-01 10:23:10, question_type_id = 2
qid = 8, uid = 1, dateposted = 2013-01-01 10:24:10, question_type_id = 2
qid = 9, uid = 1, dateposted = 2013-01-01 10:25:10, question_type_id = 2
qid = 10, uid = 1, dateposted = 2013-01-01 10:26:10, question_type_id = 2
qid = 11, uid = 1, dateposted = 2013-01-01 10:27:10, question_type_id = 2
qid = 12, uid = 2, dateposted = 2013-01-01 10:17:10, question_type_id = 1
qid = 13, uid = 2, dateposted = 2013-01-01 10:27:10, question_type_id = 1
qid = 14, uid = 1, dateposted = 2013-01-01 12:27:10, question_type_id = 1
4

1 回答 1

1

您将不得不分几个步骤进行。

假设您使用 MySQL,请检查日期和时间函数。功能HOUR()可能是您想要的:

SELECT count(id) AS cpt, HOUR(dateposted) AS hour FROM tbl_questions GROUP BY hour ORDER BY cpt DESC;

从这里您可以提取最大计数和相关小时数(至少一个,也许几个,然后检索表中的相关行。您可以构建所需小时数的数组并查询类似

UPDATE tbl_questions SET question_type_id = 2 WHERE HOUR(dateposted) IN (...your list of hours)
于 2013-05-23T16:56:17.663 回答