2014-03-05 171 views
0

我有兩個查詢選擇相似的列,但有不同的WHEREJOIN子句。我需要將這兩個查詢與UNION合併,以便總共顯示26個結果(合併),按日期排序。我不知道我會如何處理以下兩個查詢。結合兩個不同的查詢

首先查詢:

SELECT p.post_id, 
     p.reply_to, 
     p.parent_id, 
     p.post_path, 
     p.user_id, 
     p.content, 
     p.datetime, 
     p.total_likes, 
     p.total_replies, 
     p.total_reposts, 
     u.username, 
     u.display_name, 
     l.like_id, 
     l.user_id 
FROM posts p 
LEFT JOIN users u ON p.user_id = u.user_id 
LEFT JOIN likes l ON (l.post_id = p.post_id AND l.user_id = ?) 
WHERE p.user_id IN (SELECT following_id FROM follows WHERE user_id = ?) 
AND p.removed != 1 
ORDER BY p.datetime DESC LIMIT 26 

第二個查詢:

SELECT p.post_id, 
     p.reply_to, 
     p.parent_id, 
     p.post_path, 
     p.user_id, 
     p.content, 
     p.datetime, 
     p.total_likes, 
     p.total_replies, 
     p.total_reposts, 
     u.username, 
     u.display_name, 
     l.like_id, 
     l.user_id 
FROM posts p 
LEFT JOIN users u ON p.user_id = u.user_id 
LEFT JOIN likes l ON l.post_id = p.post_id 
WHERE l.user_id IN (SELECT user_id FROM likes WHERE user_id IN (SELECT following_id FROM follows WHERE user_id = ? AND following_id != ?)) 
AND l.user_id != p.user_id AND p.removed != 1 
ORDER BY p.datetime DESC LIMIT 26 

我會怎麼做呢?

+2

他們有相同的一組返回列?看起來像它一樣。只需在兩次選擇之間放下'UNION'(用於刪除dups)或'UNION ALL'。 – Andrew

+1

只是使用union all作爲andrew所說的並在查詢結束時使用'ORDER BY p.datetime DESC LIMIT 26'確保它在末尾使用一次 –

回答

1

您應該能夠在兩個select語句之間放置UNION或UNION ALL。此外,請確保您刪除第一個ORDER BY,這將引發錯誤。下面的語句應該運行。

SELECT p.post_id, 
     p.reply_to, 
     p.parent_id, 
     p.post_path, 
     p.user_id, 
     p.content, 
     p.datetime, 
     p.total_likes, 
     p.total_replies, 
     p.total_reposts, 
     u.username, 
     u.display_name, 
     l.like_id, 
     l.user_id 
FROM posts p 
LEFT JOIN users u ON p.user_id = u.user_id 
LEFT JOIN likes l ON (l.post_id = p.post_id AND l.user_id = ?) 
WHERE p.user_id IN (SELECT following_id FROM follows WHERE user_id = ?) 
AND p.removed != 1 
UNION 
SELECT p.post_id, 
     p.reply_to, 
     p.parent_id, 
     p.post_path, 
     p.user_id, 
     p.content, 
     p.datetime, 
     p.total_likes, 
     p.total_replies, 
     p.total_reposts, 
     u.username, 
     u.display_name, 
     l.like_id, 
     l.user_id 
FROM posts p 
LEFT JOIN users u ON p.user_id = u.user_id 
LEFT JOIN likes l ON l.post_id = p.post_id 
WHERE l.user_id IN (SELECT user_id FROM likes WHERE user_id IN (SELECT following_id FROM follows WHERE user_id = ? AND following_id != ?)) 
AND l.user_id != p.user_id AND p.removed != 1 
ORDER BY p.datetime DESC LIMIT 26 
+0

我得到一個錯誤'試圖獲取非對象的屬性'在PHP的這一行:'if($ stmt-> num_rows){'。 – Walter

0

您可以使用UNION。試試這個

SELECT 
    (SELECT p.post_id, 
    p.reply_to, 
    p.parent_id, 
    p.post_path, 
    p.user_id, 
    p.content, 
    p.datetime, 
    p.total_likes, 
    p.total_replies, 
    p.total_reposts, 
    u.username, 
    u.display_name, 
    l.like_id, 
    l.user_id 
    FROM posts p 
    LEFT JOIN users u ON p.user_id = u.user_id 
    LEFT JOIN likes l ON (l.post_id = p.post_id AND l.user_id = ?) 
    WHERE p.user_id IN (SELECT following_id FROM follows WHERE user_id = ?) 
    AND p.removed != 1 
    ORDER BY p.datetime DESC LIMIT 26) 

UNION 

(SELECT p.post_id, 
     p.reply_to, 
     p.parent_id, 
     p.post_path, 
     p.user_id, 
     p.content, 
     p.datetime, 
     p.total_likes, 
     p.total_replies, 
     p.total_reposts, 
     u.username, 
     u.display_name, 
     l.like_id, 
     l.user_id 
FROM posts p 
LEFT JOIN users u ON p.user_id = u.user_id 
LEFT JOIN likes l ON l.post_id = p.post_id 
WHERE l.user_id IN (SELECT user_id FROM likes WHERE user_id IN (SELECT following_id FROM follows WHERE user_id = ? AND following_id != ?)) 
AND l.user_id != p.user_id AND p.removed != 1 
ORDER BY p.datetime DESC LIMIT 26) 
+0

您也可以在沒有子選擇的情況下使用 –

+0

該問題要求提供26個組合結果。 LIMIT應該在最後,正如其他答案中所發佈的那樣。 – trogdor

+0

我在PHP的這一行中試圖獲取非對象的屬性:if($ stmt-> num_rows){'。 – Walter

0

如果所選列具有兼容的類型,只是把它們與UNION在一起。我明白你想要把LIMIT在年底將其應用到完整的查詢:

SELECT p.post_id, 
    p.reply_to, 
    p.parent_id, 
    p.post_path, 
    p.user_id, 
    p.content, 
    p.datetime, 
    p.total_likes, 
    p.total_replies, 
    p.total_reposts, 
    u.username, 
    u.display_name, 
    l.like_id, 
    l.user_id 
FROM posts p 
    LEFT JOIN users u ON p.user_id = u.user_id 
    LEFT JOIN likes l ON (l.post_id = p.post_id AND l.user_id = ?) 
WHERE p.user_id IN (SELECT following_id FROM follows WHERE user_id = ?) 
    AND p.removed != 1 
UNION 
SELECT p.post_id, 
    p.reply_to, 
    p.parent_id, 
    p.post_path, 
    p.user_id, 
    p.content, 
    p.datetime, 
    p.total_likes, 
    p.total_replies, 
    p.total_reposts, 
    u.username, 
    u.display_name, 
    l.like_id, 
    l.user_id 
FROM posts p 
LEFT JOIN users u ON p.user_id = u.user_id 
LEFT JOIN likes l ON l.post_id = p.post_id 
WHERE l.user_id IN (SELECT user_id FROM likes WHERE user_id IN 
      (SELECT following_id FROM follws WHERE user_id = ? AND following_id != ?)) 
    AND l.user_id != p.user_id AND p.removed != 1 
ORDER BY p.datetime DESC LIMIT 26 
+0

我在PHP的這一行中試圖獲取非對象的屬性:if($ stmt-> num_rows){'。 – Walter