0

我有两张桌子:

  • 职业
  • 职业详情

表“ career ”包含字段(careerID、job_code)
表“career_details”包含字段(id、careerID、name)

让价值观成为[(1,code1),(2,code2)]--- career(table)
让价值观成为

[

 ('1','1','codename1'),  
   ('2','1','codename11'),  
   ('3','2','codename22'),  
   ('4','2','codename222')


]

--career_details(表)

现在,如果我编写查询(“ SELECT * FROM career c LEFT JOIN career_details cd ON c.career_id=cd.career_id WHERE 1”),那么它将给出 4 行的结果,但我只需要 2 行的结果,即

[(1,code1,1,1,codename1),(2,code2,3,2,codename22)]
4

2 回答 2

0

而不是写左连接,你应该写内连接

再次根据您的要求,您需要首先分配职业数据的第一个职业详细信息数据。请参考以下链接。 http://www.sqlfiddle.com/#!2/d1c64/14

SELECT c.id,c.name as name ,cd.id as cdl_id,cd.career_id,cd.name as dtl_name FROM career_details cd  INNER JOIN careers c  ON c.id=cd.career_id
and cd.id in (select max(id) from career_details group by career_id);
于 2013-07-18T09:04:21.100 回答
0
SELECT * FROM career c LEFT JOIN career_details cd ON c.career_id=cd.career_id 
group by c.id

Group by c.id 将限制结果集中只有一个 c.id 条目。

于 2013-07-18T12:32:27.413 回答