SQL从不同的表中获取值

时间:2012-10-26 05:20:22

标签: mysql sql

我有2个sql表,如:

user_logs 
-id
-log_date
-log

system_logs
-id
-create_date
-log

是否可以从这些表中获取值,并按日期对它们进行排序?

4 个答案:

答案 0 :(得分:0)

您是否正在寻找类似此查询的内容

(select id, date, log from user_logs)
union
(select id, date, log from system_logs)
order by date;

更好的设计可能是使用一个具有列id,date,log和log_type的表,其中log_type定义日志是用户还是系统日志。

答案 1 :(得分:0)

SELECT id, date FROM user_logs UNION SELECT id, date from system_logs ORDER BY date DESC;

或者类似的东西,无论如何,UNION是你要找的关键字,see link

答案 2 :(得分:0)

使用UNION

尝试
SELECT *
FROM
(
    SELECT id, date, log FROM user_logs
    UNION
    SELECT id, date, log FROM system_logs
)
ORDER BY date desc

答案 3 :(得分:0)

   SELECT *
FROM
( SELECT id,
           date,
           log
    FROM system_logs

    UNION

    SELECT id,
           date,
           log
    FROM user_logs) order by date  DESC

OR

SELECT id,
               date,
               log
        FROM system_logs

        UNION

        SELECT id,
               date,
               log
        FROM user_logs order by date  DESC