[英]How to select all rows from column has different value “Select All or both”
[英]Select all and adding a new column if has certain value
我有一個platforms
表,其中包含id
, name
和parent_id
及其他列。
是否可以編寫一個查詢來從表中選擇所有platforms
,並且如果查詢的parent_id
不為0
,那么會添加一個額外的列,其parent_name
?
喜歡:
id name parent_id
2 pl1 0
3 pl2 0
4 pl3 2
所需的查詢將產生:
id name parent_id parent_name
2 pl1 0
3 pl2 0
4 pl3 2 pl1
您可以使用left join
執行此操作:
select p.*, pp.name as parent_name
from platforms p left join
platforms pp
on p.parent_id = pp.id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.