Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

mysql - How can I force a subquery return only one row?

Here is my query:

SELECT e.id, e.table_code,

 ( SELECT MIN(date_time) date_time,
         SUM(score) score,
         type,
         post_id,
         table_code,
         comment_id,
         context
   FROM events 
   WHERE author_id = 32
   GROUP BY type, post_id, table_code, comment_id, context
   ORDER BY MIN(date_time) DESC
   LIMIT 15 ) g

FROM events e
WHERE e.author_id = 32 AND e.date_time >= MIN(g.date_time)
ORDER BY seen ASC, date_time DESC 

It throws:

#1241 - Operand should contain 1 column(s)

Any idea how can I fix it?


I'm trying to:

  1. Select 15 groups of latest events (the smallest id of groups)
  2. And then Select all events (which are belong to a specific user) that have a bigger id that that smallest id

Noted that, probably "group" be vague. Actually it's about notifications, so the notifications which has the same post id (and are related to vote) will be summed and named as one group.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use LEFT JOIN instead of Sub-Query.

Try this:

SELECT e.id, e.table_code,g.*
FROM events e
LEFT JOIN (SELECT MIN(date_time) date_time,
         SUM(score) score,
         type,
         post_id,
         table_code,
         comment_id,
         context
   FROM events 
   WHERE author_id = 32
   GROUP BY type, post_id, table_code, comment_id, context
   ORDER BY MIN(date_time) DESC
   LIMIT 15 ) g ON e.table_code = g.table_code AND e.date_time >= g.date_time
WHERE e.author_id = 32
ORDER BY seen ASC, e.date_time DESC

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...