SQL建立臨時表

flash_love發表於2017-09-05

在資料庫查詢資料的時候,比如查詢出type需要根據不用的值,換一個別名,在程式中迴圈處理有點麻煩,現在有一個方法就是用臨時表跟查詢表連線查詢即可。所以現在需要新建一個臨時表,然後用左聯接聯合查詢。

1、新建臨時表

select '100' as val,'sc' as lx 
 UNION select '200' as val,'zy' as lx
 UNION select '300' as val,'bg' as lx 
 UNION select '600' as val,'yy' as lx 

2、連線查詢

select ty.*,ta.* from table ta 
 left join (select '100' as val,'sc' as lx 
 UNION select '200' as val,'zy' as lx
 UNION select '300' as val,'bg' as lx 
 UNION select '600' as val,'yy' as lx ) ty on ty.lx=ta.lx

相關文章