我的表:
tbl_questions =>
qid
question
detail
mcid
cid
uid
answercount
dateposted
status
showname
emailnotify
question_type_id
我想按小时分组所有行。 {dateposted = Y-m-d 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
,Cos超过一小时限制。我需要在这些行中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
。
所以预期的输出是: tbl_questions =>
{{1}}
答案 0 :(得分:1)
您必须完成几个步骤。
假设您使用MySQL,请检查date & time functions。功能 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)