左外连接+查询条件+排序的SQL写法

SELECT DISTINCT a.*,GROUP_CONCAT(c.project_name) as projectNames 
FROM pg_contract_info a   
 
LEFT JOIN pg_contract_project b ON a.id = b.contract_id 
LEFT JOIN pg_project_info c ON b.project_id = c.id 

where 1=1  and a.contract_status in (1,2) and a.paper_contract_no LIKE 12345

GROUP BY a.id ORDER BY created_datetime DESC

说明一下:a,b,c是表的别名,a是主表。查询条件放在条件连接查询后面,多个条件用and 隔开,可以一直加and。该查询语句亲测无误。

你可能感兴趣的:(关系型数据库)