MYSQL UNION ORDER BY

时间:2012-04-11 12:41:46

标签: php mysql

我目前有这个

SELECT  *
FROM    images
WHERE   images.id IN (SELECT image_id FROM image_likes WHERE user_id = '1')
    UNION
SELECT * 
FROM images
WHERE images.user_id = '1' AND upload_type in (4,3) ORDER BY id DESC

这很有效,但现在我希望它显示最近发生的结果。 例如,用户A上传了图像 - >信息带有时间戳进入images。用户A然后喜欢别人的图像 - >信息带有时间戳进入image_likes。现在我如何将两个时间戳列合并在一起,以便我可以通过DESC对它们进行排序。

Breif继续查询查询的内容。 从用户喜欢images中的图片的image_likes中选择图像信息,然后抓取用户从images上传的所有上传内容并将其合并到查询中。我需要的是能够使用两个表中的timestamp对它们进行排序。

4 个答案:

答案 0 :(得分:3)

这样做怎么样?

SELECT * FROM (
    SELECT  *
    FROM    images
    WHERE   images.id IN (SELECT image_id FROM image_likes WHERE user_id = '1')
        UNION
    SELECT * 
    FROM images
    WHERE images.user_id = '1' AND upload_type in (4,3) ORDER BY id DESC
) AS a ORDER BY a.timestamp DESC;

或更好

 (SELECT  *
    FROM images
    WHERE images.id IN (SELECT image_id FROM image_likes WHERE user_id = '1'))
        UNION
 (SELECT * 
    FROM images
    WHERE images.user_id = '1' AND upload_type in (4,3) ORDER BY id DESC)
 ORDER BY timestamp DESC

chk this http://dev.mysql.com/doc/refman/5.5/en/union.html

<强>更新

尝试这个

 (SELECT images.id, images_likes.timestamp as timestamp FROM images JOIN images_likes 
    ON images.id=image_likes.image_id WHERE user_id = '1')
        UNION
 (SELECT images.id, images.timestamp as timestamp
    FROM images
    WHERE images.user_id = '1' AND upload_type in (4,3) ORDER BY id DESC)
 ORDER BY timestamp DESC

<强>更新

根据您的要求进行最终查询

 (SELECT images.id, images.user_id, images.ext, images.upload_type, images_likes.timestamp as timestamp FROM images JOIN images_likes 
    ON images.id=image_likes.image_id WHERE images_likes.user_id = '1')
        UNION
 (SELECT images.id, images.user_id, images.ext, images.upload_type, images.timestamp as timestamp
    FROM images
    WHERE images.user_id = '1' AND upload_type in (4,3) ORDER BY id DESC)
 ORDER BY timestamp DESC

答案 1 :(得分:1)

我将使用以下内容...下面的“PreQuery”以用户首先喜欢的所有图像开始,最大时间戳作为新列“MostRecent”。然后,为下一组做一个UNION ALL。下一组直接来自用户的图像表,但是LEFT JOINED到“喜欢”的图像。然后WHERE子句显式排除那些使用“iml2.image_id IS NULL”在第一个集合中返回的子句。我之所以这样做是因为我对第一组联盟如何处理小组没有肯定,但是在第二组中没有小组。例如:如果图像是使用3月3日的日期/时间戳创建的,但也被发现为3月5日的“赞”,我不希望将一个实例列为每个不同的日期(因为联合已经限定了明显的隐含)。 / p>

也就是说,我会根据最近的日期/时间戳,以及他们自己的个人图像及其各自的日期/时间戳,获得任何图像的预先查询结果。现在,它简单地连接回图像表以获取细节(通过别名“i2”),我们可以按PreQuery.MostRecent列排序,然后按图像ID值降序排序。

SELECT
      i2.*
   from
      ( select iml.image_id as id,
               max( iml.YourTimeStamp ) as MostRecent
           from image_likes iml
           where iml.user_id = '1'
           group by iml.image_id

        UNION ALL

        select i.id,
               i.ImageTimeStamp as MostRecent
           from images i
              left join image_likes iml2
                 on i.id = iml2.image_id
                 and iml2.user_id = '1'
           where i.user_id = '1'
             and i.upload_type in ( 4, 3 )
             and iml2.image_id is null 
      ) PreQuery
         JOIN Images i2
            on PreQuery.id = i2.id
   order by
      PreQuery.MostRecent,
      PreQuery.ID desc

答案 2 :(得分:0)

为什么不使用'OR'?

那样:

  SELECT  *
  FROM    images
  WHERE   images.id IN (SELECT image_id FROM image_likes WHERE user_id = '1')
      OR  (images.user_id = '1' AND upload_type in (4,3))
 ORDER BY id DESC

答案 3 :(得分:0)

SELECT *
FROM 
  ( SELECT  im.*
          , li.timestamp AS ts
    FROM    images AS im 
      JOIN  image_likes AS li
        ON  li.image_id = im.image_id
    WHERE   li.user_id = 1
  UNION ALL
    SELECT *
         , timestamp AS ts
    FROM images 
    WHERE user_id = 1 
      AND upload_type in (4,3) 
  ) AS tmp
ORDER BY ts DESC