根据第一个表记录组合两个表

时间:2015-02-07 06:24:15

标签: python mysql join

我有两张这样的表,

  Class  |  Name     | Unit  | Year
 --------+-----------+-------+--------
  10th   |  Language |  2    | 2014
  10th   |  Maths    |  3    | 2014

第二张表,

      Name  | Title | Period | Syllabus
  ----------+-------+--------+-----------------------------
   Language |Unit1  | 8      | Subject1, Subject2, Subject3
   Language |Unit2  | 9      | Subject4, Subject5, Subject6
    Maths   |Unit1  | 7      | Subject1, Subject2, Subject3
    Maths   |Unit2  | 8      | Subject4, Subject5, Subject6
    Maths   |Unit3  | 9      | Subject5, Subject6, Subject7

结果应该是,

      Language

      Title | Period | Syllabus
    ----------------------------------
     Unit1  | 8      | Subject1, Subject2, Subject3
     Unit2  | 9      | Subject4, Subject5, Subject6

      Maths

      Title | Period | Syllabus
    ----------------------------------
     Unit1  | 7      | Subject1, Subject2, Subject3
     Unit2  | 8      | Subject4, Subject5, Subject6
     Unit3  | 9      | Subject5, Subject6, Subject7

第二个表结果基于第一个表名..

任何人都可以用mysql帮我解决python问题吗?我非常喜欢python ..

1 个答案:

答案 0 :(得分:0)

--To get language details:
SELECT title, period, syllabus
FROM Second
WHERE Name = 'Language' --replace language with Maths here to get maths subject details

--To get details from table1 and table2
SELECT s.title, s.period, s.syllabus, r.year
FROM Second s INNER JOIN First f
ON f.Name = s.Name
WHERE Name = 'Language' --replace language with Maths here to get maths subject details