简体   繁体   中英

Find the max record in two tables

I have two tables:

 question(qid int,title varchar(100))

 answer(aid int,qid int,vote int,content varchar(4096))

Qid and aid are primary key.

Each question has some answers, and each answer has an vote number.

Now taking some question qid, how to find the max(vote) answer for each question if the question has some answers?

Ex:

mysql> select * from question;
+-----+-------+
| qid | title |
+-----+-------+
|   1 | abc   |
|   2 | efg   |
|   3 | hij   |
|   4 | mn    |
+-----+-------+

mysql> select * from answer;
+-----+------+------+---------+
| aid | qid  | vote | content |
+-----+------+------+---------+
|  77 |    3 |   45 | mysql2  |
| 110 |    1 |   95 | good    |
| 122 |    1 |   78 | bad     |
| 123 |    1 |   34 | bad2    |
| 223 |    2 |   56 | book1   |
| 224 |    2 |   82 | book2   |
+-----+------+------+---------+

Now, giving qid(1,2), I want to find the result following:

+-----+------+------+---------+
| aid | qid  | vote | content |
+-----+------+------+---------+
| 110 |    1 |   95 | good    |
| 224 |    2 |   82 | book2   |
+-----+------+------+---------+

I want the full answer record(with all columns), not only the max(vote) column.

I just want to show the best answer(the max vote answer) for each question.

What's the best SQL in MySQL ?

Thank you!


Update 2013/1/7:

I want to show the only one best answer (the max vote answer) if exists. The answer from @Brian Hoover works fine ONLY under MySQL.

Maybe there is not a SQL working fine under all databases.

This is one way to do it.

SELECT answer.aid, answer.qid, answer.vote, answer.content
   FROM answer
   JOIN (
          SELECT qid, max(vote) vote FROM answer
          GROUP BY qid) AS max_answer
    ON answer.qid = max_answer.qid AND answer.vote = max_answer.vote
   where answer.qid in (1,2)
   GROUP BY answer.qid, answer.vote

SQL Fiddle

In this case, ties will be broken, pretty much at random, so only one record will show per questionID, but there is no guarantee that the answer chosen in the case of tie will be consistent

SELECT a.* FROM answer a
INNER JOIN (
  SELECT qid, MAX(vote) AS max_vote
  FROM answer
  WHERE qid IN (1,2)
  GROUP BY qid) b
  ON a.qid = b.qid
  AND a.vote = b.max_vote

SQL Fiddle

I think this does what you want:

select a.*
from answer a
where a.qid in (1, 2) and
      a.vote = (select max(vote) from answer a2 where a2.qid = a.qid)

This version would work in any database. There are other ways to express this in standard SQL:

select a.*
from answer a join
     (select a.qid, max(vote) as maxvote
      from answer a
      where a.qid in (1, 2)
      group by a.qid
     ) asum
     on a.qid = asum.qid and a.vote = asum.maxvote

Notice that I put the where restriction in the subquery. Otherwise, the query will need to do the group by on all the rows in answer, then join, then do the filtering.

Please try this query:

select aid,qid,max(vote),content
from
(select a.aid aid,a.qid qid,a.vote vote,a.content content 
from question q join
answer a
on (a.qid = q.qid)
order by 3 desc) tbl
group by qid;
select * from (
  select qid, aid ,vote
  from answers where qid  in ( 123, 456 ) 
  order by vote desc 
) aaa
group by qid

If some records have has same vote, this works too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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