调用一个表的信息我知道"select * from 表名 where 条件 order by 排序字段 desc limit 10"如果从两个表内调用10条最新消息怎么写? 后面是order by newstime desc limit 10 我会,关键前面怎么写?能实现吗?

解决方案 »

  1.   

    两个结果UNION不行?
    select * from 
    (select * from 表名 where 条件 order by 排序字段 desc limit 10)
    union
    select * from 
    (select * from 表名 where 条件 order by 排序字段 desc limit 10)
      

  2.   

    select * from A inner Join B on A.id=B.id where 条件 order by newstime desc limit 10
      

  3.   

    估计是想这样。猜的。select * from 
    (
    select * from 表名1 where 条件
    union all
    select * from 表名2 where 条件

    order by 排序字段 desc limit 10
      

  4.   


    这样是不行的!!!UNION逻辑操作不能与ORDER BY操作同时出现在一个语句中的。
    参考四楼的代码,是可以的!!变通一下啊。
      

  5.   

    4楼代码:SELECT * 
    FROM (
    SELECT * 
    FROM phome_ecms_info
    UNION ALL SELECT * 
    FROM phome_emcs_house
    )
    ORDER BY newstime DESC 
    LIMIT 10 MySQL 返回: #1248 - Every derived table must have its own alias 实在是不懂SQL, 我加上as,错误还是一样。
    表的结构看这里的 4楼 http://topic.csdn.net/u/20090322/15/160a3e85-d0f3-4f80-a9f7-106e2c6417c0.html被这个东西弄得头都大了
      

  6.   

    SELECT *
    FROM (
    SELECT A.title AS A_title,A.area as.A_area,A.newstime AS A_newstime 
    FROM phome_ecms_info A
    UNION ALL 
    SELECT B.title AS B_title,B.area as.B_area,B.newstime AS B_newstime
    FROM phome_emcs_house B
    )
    ORDER BY A_newstime DESC,B_newstime DESC
    LIMIT 10 
      

  7.   

    已经解决了,是用union,不是union all,是类似4楼。如果有人不懂,查查union 用法就可以了。没人对,平均给分八