将两个单独的sql查询合并为单个查询

时间:2013-01-24 08:31:50

标签: mysql sql

我有两个SQL查询

SELECT sub_id, 
       sub_sent_code, 
       date_time 
FROM   moviedatabase.reg_sub_master 
WHERE  (date_time >= '2013-01-19' 
         AND date_time <= '2013-01-29');

SELECT sub_id, 
       sub_sent_code, 
       date_time 
FROM   moviedatabase.reg_sub_master 
WHERE  date_time <= '2013-01-19'; 

我的要求来自第一个查询,结果是sub_id,sub_sent_code,date_time不应出现在第二个查询中。

我想让它成为单一查询。

编辑:实际上我想要的结果是date_time >='2013-01-19'date_time <='2013-01-29'以及我得到的sub_id不应该在{{1}之前出现}

1 个答案:

答案 0 :(得分:4)

尝试:

SELECT sub_id,sub_sent_code,date_time
FROM   moviedatabase.reg_sub_master
WHERE  (date_time >='2013-01-19' 
    AND date_time <='2013-01-29')
    AND sub_id NOT IN (
        SELECT DISTINCT sub_id
        FROM   moviedatabase.reg_sub_master
        WHERE  date_time <='2013-01-19;