2016-07-29 95 views
0

每一個UNION ALL不選擇更3列

我婉通過UNION ALL在PHP

選擇從兩個表8列當我選擇3列,它的工作原理 但是當我的工作選擇4個或更多(像5,6,7,8),它不選擇3

SELECT ID , NULL AS Title , Who 
FROM life 
UNION ALL 
SELECT ID , Title , NULL AS Who 
FROM content 

我的查詢工作

我的查詢中選擇更

SELECT ID , NULL AS Title , Who , MainImage , SubImage , NULL AS Place , ENDate , NULL AS Date 
FROM life 
UNION ALL 
SELECT ID , Title , NULL AS Who , MainImage , SubImage , Place , ENDate , Date 
From content 
ORDER BY ENDate DESC 

我嘗試這些但不是有效的

SELECT life.ID , NULL AS Title , life.Who 
FROM life 
UNION ALL 
SELECT content.ID , content.Title , NULL AS Who 
FROM content 

SELECT life.ID , NULL AS Title , life.Who , life.MainImage , life.SubImage , NULL AS Place , life.ENDate , NULL AS Date 
FROM life 
UNION ALL 
SELECT content.ID , content.Title , NULL AS Who , content.MainImage , content.SubImage , content.Place , content.ENDate , content.Date 
From content 
ORDER BY ENDate DESC 
+2

什麼是「不工作」呢? –

+0

在頂部查詢中有'CONVERT(DATETIME,NULL)AS Date'嗎? – HoneyBadger

回答

0

Date是SQL保留關鍵字。你應該使用反勾號字符(`)。

對於後面的勾號,請參考https://superuser.com/questions/254076/how-do-i-type-the-tick-and-backtick-characters-on-windows

+0

NOW 它的工作原理 $ my_query =「SELECT NULL AS AS,zendegi.ENDate,NULL AS日期從zendegi UNION ALL SELECT content.Place,content.ENDate,content.Date從內容ORDER BY ENDATE DESC」; –

+0

@ Mr.Kodiran,這是一個完全不同的查詢,然後在你原來的問題中......(你還沒有使用反引號) – HoneyBadger