ホームページ  >  に質問  >  本文

请教一条mysql的sql语句写法;

现在是有两张表;
msg:针对单个用户的普通消息通知;
admin_msg:管理员发送给全部用户的消息通知;

mysql> select * from msg;

id content date
1 内容1 2016-08-03 11:56:54
2 内容3 2016-08-05 11:57:06

2 rows in set

mysql> select * from admin_msg;

id content date
1 内容2 2016-08-04 11:56:41
2 内容4 2016-08-06 11:57:19

2 rows in set

我想要的效果是当执行 [此处是一段sql] order by date asc limit 0,2 的时候能获取到meg中的 内容1 和 admin_msg中的 内容2 ;

那么问题来了;挖掘机;额不对;请问这段sql应该怎么写?

先感谢各位大神的解答;

ringa_leeringa_lee2742日前629

全員に返信(2)返信します

  • ringa_lee

    ringa_lee2017-04-17 15:21:25

    select content, date from (select content, date from msg union all select content, date from admin_msg) subquery order by date asc limit 0, 2;

    返事
    0
  • 天蓬老师

    天蓬老师2017-04-17 15:21:25

    SELECT a.content as content1,b.content as content2 
            FROM  msg AS a LEFT JOIN admin_msg AS b 
            ON a.id = b.id 
            WHERE a.id=1  
            ORDER BY a.date ASC LIMIT 2;
    

    用到了左链接,献丑了

    返事
    0
  • キャンセル返事