select r.id, IF(c.name != '',c.name,r.name) as name, r.age FROM Records r LEFT JOIN Clients c ON c.id = r.id GROUP BY c.id
使用上面的查询。
编辑:
SELECt t.id, t.name, t.age FROM ( ( SELECt r.id, CASE WHEN c.name <> '' THEN c.name ELSE r.name END as name, r.age FROM Records r LEFT JOIN Clients c ON c.id = r.id ) UNIOn ( SELECt c.id, c.name, null as age FROM Clients c where c.id NOT IN (select id from Records) )) as t ORDER BY t.id
使用此查询。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)