【问题标题】:order by with union in php在 php 中使用 union 排序
【发布时间】:2018-07-23 10:54:01
【问题描述】:

我想使用 order by 语句来为我提供 top 的结果。

$sql_ins = "select user_first_name, user_last_name, user_id from user 
            INNER JOIN xyz on user_id=s_id 
            where (s_id='".$_SESSION['user']."' OR r_id='".$_SESSION['user']."')
              AND user_id!='".$_SESSION['user']."' group by user_id

            UNION

            SELECT user_first_name, user_last_name, user_id from user 
            INNER JOIN xyz on user_id=r_id 
            WHERE (r_id='".$_SESSION['user']."' OR s_id='".$_SESSION['user']."')
              AND user_id!='".$_SESSION['user']."' 

            GROUP BY user_id";

我想按 id desc 的顺序获取输出,但输出是错误的。

【问题讨论】:

  • 请在echo $sql_ins 之后提供最终的 SQL 查询。你得到什么错误。没有它,任何人都无法提供帮助。

标签: php mysql sql-order-by union


【解决方案1】:

使用这个对我有用的查询....

SELECT * 
FROM (SELECT content_update.id as id, 
             content_update.course_id as course_id, 
             content_update.status as status, 
             content_update.title as title, 
             content_update.img_src as img, 
             content_update.entry_date as entry_date 
      FROM content_update 

      UNION ALL 

      SELECT video_update.id as id, 
             video_update.course_id as course_id, 
             video_update.status as status,
             video_update.title as title, 
             video_update.video_link as img, 
             video_update.entry_date as entry_date 
      FROM video_update ) results 

ORDER BY entry_date DESC

【讨论】:

  • @AshishYadav 老兄,请小心使用它对我有用,我在我的 Live 项目中使用它
猜你喜欢
  • 2011-06-10
  • 2016-02-13
  • 2015-05-14
  • 1970-01-01
  • 2016-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多