如果它们存在于第二个MySQL查询中,如何从第一个MySQL查询中删除结果?

时间:2014-10-22 09:06:51

标签: mysql sql database mysql-error-1064

如果第一个查询中的结果存在于第二个查询中,如何删除结果?

我查看过以前的stackoverflow线程但仍无法使其工作。

到目前为止,我的MySQL个查询位于以下位置:

SELECT roomL.CityCode, roomL.TableCode
FROM (SELECT roomL.CityCode, roomL.TableCode 
      FROM roomL
      WHERE roomL.StatusEnum = 1 
      AND roomL.TableCode NOT IN (select stock.TableCode from stock)
      AND roomL.TimeSeen BETWEEN 10/10/2014 AND Now())

WHERE NOT EXISTS (SELECT roomL.CityCode , roomL.TableCode 
                  FROM roomL
                  WHERE roomL.StatusEnum = 1 
                  AND roomL.TableCode NOT IN (select stock.TableCode from stock)
                  AND roomL.TimeSeen BETWEEN DATE_ADD( 17/10/2014 ,INTERVAL 1 DAY) AND Now())

目前我收到错误:

SQL错误(1248):每个派生表必须有自己的别名

2 个答案:

答案 0 :(得分:-1)

首先使用正确的数据类型更改表格,您需要首先添加额外的列:

ALTER table roomL
ADD COLUMN TimeSeen1 DATE;

第二次从字符串im TimeSeen

更新新列
UPDATE roomL
SET TimeSeen1 = STR_TO_DATE(TimeSeen, '%d/%m/%Y');

第三个删除旧列

ALTER TABLE roomL
DROP COLUMN TimeSeen;

最后重命名新列(可选)

ALTER TABLE roomL
CHANGE COLUMN TimeSeen1 TimeSeen DATE;

然后你可以稍微调整一下使用Strawberry的查询:

SELECT
     r.CityCode,
     r.TableCode
FROM
    roomL r
LEFT JOIN
    stock s
    ON r.TableCode = s.TableCode
WHERE
    r.Status_Enum = 1 AND
    r.TimeSeen BETWEEN '2014-10-10' AND '2014-10-18'
    AND s.TableCode IS NULL

答案 1 :(得分:-1)

试试这个:

SELECT temp1.CityCode, temp1.TableCode
FROM (SELECT roomL.CityCode, roomL.TableCode 
      FROM roomL
      WHERE roomL.StatusEnum = 1 
      AND roomL.TableCode NOT IN (select stock.TableCode from stock)
      AND roomL.TimeSeen BETWEEN '2014-10-10' AND Now())temp1

left join (SELECT roomL.CityCode , roomL.TableCode 
                  FROM roomL
                  WHERE roomL.StatusEnum = 1 
                  AND roomL.TableCode NOT IN (select stock.TableCode from stock)
                  AND roomL.TimeSeen BETWEEN DATE_ADD( '2014-10-17' ,INTERVAL 1 DAY) AND Now())temp2 
ON temp1.CityCode=temp2.CityCode AND temp1.TableCode=temp2.TableCode
WHERE temp2.CityCode IS NULL AND temp2.TableCode IS NULL;