2012-12-08 29 views
0

表結構: enter image description hereMySQL選擇語法給了我每個id輸出兩行是什麼錯誤?

SELECT p.id id, p.type 
    TYPE , v.provider provider, v.video_id video_id, NULL content, NULL url, NULL posted_by 
    FROM post p 
    INNER JOIN p_video v ON p.id = v.id 
    AND p.type = 'video' 
    UNION 
    SELECT p.id, p.type, NULL , NULL , t.content, NULL , NULL 
    FROM post p 
    INNER JOIN p_text t ON p.id = t.id 
    AND p.type = 'text' 
    UNION 
    SELECT p.id, p.type, NULL , NULL , NULL , i.url, NULL 
    FROM post p 
    INNER JOIN p_image i ON p.id = i.id 
    AND p.type = 'image' 
    UNION 
    SELECT p.id, p.type, NULL , NULL , NULL , l.url, NULL 
    FROM post p 
    INNER JOIN p_link l ON p.id = l.id 
    AND p.type = 'link' 
    UNION 
    SELECT p.id, NULL , NULL , NULL , NULL , NULL , u.name 
    FROM post p 
    LEFT JOIN user u ON p.user_id = u.id 
    ORDER BY id DESC 
    LIMIT 0 , 30 

mySQL output

什麼是我的語法錯誤?它只是2行......但我希望所有的輸出在單行1行。

+0

這絕對看起來像一個奇怪的方法。 'UNION'負責重複行。請張貼您的餐桌結構,我們可以提供幫助。 –

+0

我無法解釋這種行爲。我張貼了一些主表。其他表就像在SQL語句中一樣。 – Tomkay

+0

當我們要求發佈表結構時,請將SHOW CREATE TABLE的輸出,而不是表格的屏幕圖片。 –

回答

1

試試這個

SELECT p.id id, p.type 
TYPE , v.provider provider, v.video_id video_id, NULL content, NULL url, u.name posted_by 
FROM post p 
LEFT JOIN user u ON p.user_id = u.id 
INNER JOIN p_video v ON p.id = v.id 
AND p.type = 'video' 
UNION 
SELECT p.id, p.type, NULL , NULL , t.content, NULL , u.name 
FROM post p 
LEFT JOIN user u ON p.user_id = u.id 
INNER JOIN p_text t ON p.id = t.id 
AND p.type = 'text' 
UNION 
SELECT p.id, p.type, NULL , NULL , NULL , i.url, u.name 
FROM post p 
LEFT JOIN user u ON p.user_id = u.id 
INNER JOIN p_image i ON p.id = i.id 
AND p.type = 'image' 
UNION 
SELECT p.id, p.type, NULL , NULL , NULL , l.url, u.name 
FROM post p 
LEFT JOIN user u ON p.user_id = u.id 
INNER JOIN p_link l ON p.id = l.id 
AND p.type = 'link' 
ORDER BY id DESC 
LIMIT 0 , 30 
+0

dis是馬朋友的正確答案。 – Tomkay

0

這是你最後一次工會選擇,這增加了第二排。如果你想要每行都加左加入到每個工會選擇部分,並省略最後一個工會選擇

SELECT p.id id, p.type 
TYPE , v.provider provider, v.video_id video_id, NULL content, NULL url, u.name posted_by 
FROM post p 
INNER JOIN p_video v ON p.id = v.id 
AND p.type = 'video' 
LEFT JOIN user u ON p.user_id = u.id 
UNION 
SELECT ... 
... 
ORDER BY id DESC 
LIMIT 0 , 30 
相關問題