繁体   English   中英

MYSQL:如何使其在选择查询中成为虚拟列?

[英]MYSQL: How to make it virtual column in select Query?

以下是任何人有主意如何解决此问题的我的问题:

查询示例:

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值?

谢谢

使用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

猜猜你需要这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM