简体   繁体   English

MySQL 插入 SELECT。 按计数排序插入,但实际上不插入计数

[英]MySQL INSERT SELECT. Ordering insert by count but don't actually insert the count number

Sorry for the ridiculous title!对不起这个荒谬的标题!

Is it possible to run a MySQL INSERT SELECT query where the 'SELECT' portion includes a COUNT() function (to benefit from a HAVING clause) -- this creates an unmatching column count on the INSERT portion.是否可以运行 MySQL INSERT SELECT 查询,其中“SELECT”部分包含 COUNT() function(受益于 HAVING 子句上的匹配列)

For example:例如:

INSERT INTO my_log (user_id, date_finished) 
SELECT COUNT(id) AS count, user_id, '2011-05-31 00:00:00'
FROM assignments WHERE assignment_type = 10 
GROUP BY user_id
HAVING count >=10 

I want to insert into the TABLE my_log a date for users who've completed 10 assignments of the type 10. In reality I'm expanding on this slightly so it's important I use the COUNT and HAVING to find only users who have completed 10 or more assignments.我想在 TABLE my_log 中插入完成 10 个类型为 10 的任务的用户的日期。实际上,我正在对此稍作扩展,因此重要的是我使用 COUNT 和 HAVING 仅查找已完成 10 个或更多的任务。 But my problem remains that I'm trying to insert 3 columns into a declared 2 columns because of the COUNT() function, so I'm looking for a way to benefit from the COUNT but not return it in my data set / considered in the insertion.但是我的问题仍然是,由于 COUNT() function,我试图将 3 列插入到声明的 2 列中,所以我正在寻找一种从 COUNT 中受益但不在我的数据集中返回它的方法/考虑插入。

Thanks for any help:)谢谢你的帮助:)

Try with:尝试:

INSERT INTO my_log ( user_id, date_finished) 
SELECT user_id, '2011-05-31 00:00:00'
FROM assignments WHERE assignment_type = 10 
GROUP BY user_id
HAVING ( COUNT(id) ) >=10 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM