[英]MYSQL: How to make it virtual column in select Query?
Below is the my question any one have idea how to resolve this issue: 以下是任何人有主意如何解决此问题的我的问题:
Example 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
Basically i don't want to write two times "global_id" column in the select statement and also in the some rows have id = NULL 基本上我不想在select语句中写两次“ global_id”列,并且在某些行中也有id = NULL
Can you please write working query here. 你能在这里写工作查询吗? How can i get id value of all rows in the global_id column ? 如何获取global_id列中所有行的ID值?
Thanks 谢谢
Use COALESCE
: 使用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
Guess you need this: 猜猜你需要这个:
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.