我有两张桌子:
表" 职业"包含字段(careerID,job_code)
表" career_details"包含字段(id,careerID,name)
让值为[(1,code1),(2,code2)]
---职业(表)
让值为
[
('1','1','codename1'),
('2','1','codename11'),
('3','2','codename22'),
('4','2','codename222')
]
- career_details(table)
现在如果我写了查询(" 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)]
答案 0 :(得分: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);
答案 1 :(得分: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的一个条目。