使用嵌套查询获取两个表的详细信息

时间:2016-01-28 13:04:43

标签: mysql sql nested

    TABLE 1                                 TABLE 2
id   name    mob                    id      course      mark    
1    joe     0000                    1      English     77
2    john    0000                    2      maths       89

我需要使用嵌套查询显示表1中具有MAX(等级)的人的姓名。

SELECT t1.name
    FROM t1
    WHERE t1.id = t2.id = (

    SELECT id
    FROM t2 
    WHERE mark = 
     (
    SELECT MAX(mark)
    FROM t2
         ) 
);

1 个答案:

答案 0 :(得分:0)

嗯,这满足了简要说明; - ):

SELECT a.* 
  FROM table_a a 
  JOIN (SELECT * FROM table_b) b 
    ON b.id = a.id 
 ORDER   
    BY mark DESC 
 LIMIT 1;