结合两个不同的查询

时间:2014-03-05 20:58:20

标签: mysql

我有两个选择类似列的查询,但有不同的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

我该怎么做?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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)

答案 2 :(得分: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