0

以下是我的问题,任何人都知道如何解决此问题:

示例查询:

SELECT id, table2.id as global_id, table3.id as global_id 
FROM table1 
LEFT JOIN table2 
  ON ( table1.id = table2.tab1_id ) 
LEFT JOIN table3 
  ON ( table1.id = table3.tab1_id ) 
WHERE etc etc

基本上我不想在 select 语句中写两次“global_id”列,并且在某些行中有 id = NULL

你能在这里写下工作查询吗?如何获取 global_id 列中所有行的 id 值?

谢谢

4

2 回答 2

0

使用COALESCE

SELECT table1.id, COALESCE(table2.id, table3.id) as global_id 
FROM table1 
LEFT JOIN table2 
  ON ( table1.id = table2.tab1_id ) 
LEFT JOIN table3 
  ON ( table1.id = table3.tab1_id ) 
WHERE etc etc
于 2013-05-16T08:58:59.013 回答
0

猜你需要这个:

SELECT id, table2.id as global_id
FROM table1 
LEFT JOIN table2 
  ON ( table1.id = table2.tab1_id ) 
WHERE etc etc
UNION ALL
SELECT id, table3.id as global_id 
FROM table1 
LEFT JOIN table3 
  ON ( table1.id = table3.tab1_id ) 
WHERE etc etc
于 2013-05-16T09:00:11.740 回答