我有两个表,一个ORDERS
,另一个ORDERITEM
ORDERS
表如下所示:
ORDERNO
ORDERDATE
ORDERREADYDATE
CUSTOMERNO
ORDERITEMS
表如下所示:
ORDERNO
ORDERDATE
QTY
ITEMNO
我有一个订单记录:
12335
11/04/2018
11/06/2018
9999999
12336
11/04/2018
11/06/2018
9999998
12337
11/04/2018
11/06/2018
9999997
12345
11/05/2018
11/07/2018
9999999
12346
11/05/2018
11/07/2018
9999998
12347
11/05/2018
11/07/2018
9999997
和订单项:
12335
11/04/2018
5
678
12335.1
11/04/2018
11
587
12335.2
11/04/2018
3
554
12336
11/04/2018
5
678
12336.1
11/04/2018
11
587
12336.2
11/04/2018
3
554
12345
11/05/2018
5
678
12345.1
11/05/2018
11
587
12345.2
11/05/2018
3
554
12346
11/05/2018
5
678
12346.1
11/05/2018
11
587
12346.2
11/05/2018
3
554
在准备好的日期,我想查找发票上没有点的所有项目。
我尝试了这个,但是它给了我旧订单:
SELECT
i.orderno,
i.orderdate,
d.orderno,
d.orderdate,
d.itemno
FROM
ORDERS i,
ORDERITEM d
WHERE
i.ORDERREADYDATE = '2018-11-07'
AND d.orderdate = i.orderdate
AND d.orderno NOT LIKE '%.%'
以下是重现该问题的代码:
TRY DROP TABLE #ORDERS; CATCH ALL END TRY;
TRY DROP TABLE #ORDERITEM; CATCH ALL END TRY;
CREATE TABLE
#ORDERS
(
ORDERNO NVARCHAR(10),
ORDERDATE DATE,
ORDERREADYDATE DATE,
CUSTOMERNO INTEGER
);
CREATE TABLE
#ORDERITEM
(
ORDERNO NVARCHAR(10),
ORDERDATE DATE,
QTY INTEGER,
ITEMNO INTEGER,
);
INSERT INTO
#ORDERS
SELECT
'12335',
'2018-11-04',
'2018-11-06',
9999999
FROM system.iota
UNION SELECT
'12336',
'2018-11-04',
'2018-11-06',
9999998
FROM system.iota
UNION SELECT
'12337',
'2018-11-04',
'2018-11-06',
9999997
FROM system.iota
UNION SELECT
'12345',
'2018-11-05',
'2018-11-07',
9999999
FROM system.iota
UNION SELECT
'12346',
'2018-11-05',
'2018-11-07',
9999998
FROM system.iota
UNION SELECT
'12347',
'2018-11-05',
'2018-11-07',
9999997
FROM system.iota;
INSERT INTO
#ORDERITEM
SELECT
'12335',
'2018-11-04',
5,
678
FROM system.iota
UNION SELECT
'12335.1',
'2018-11-04',
11,
587
FROM system.iota
UNION SELECT
'12335.2',
'2018-11-04',
3,
554
FROM system.iota
UNION SELECT
'12336',
'2018-11-04',
5,
678
FROM system.iota
UNION SELECT
'12336.1',
'2018-11-04',
11,
587
FROM system.iota
UNION SELECT
'12336.2',
'2018-11-04',
3,
554
FROM system.iota
UNION SELECT
'12345',
'2018-11-05',
5,
678
FROM system.iota
UNION SELECT
'12345.1',
'2018-11-05',
11,
587
FROM system.iota
UNION SELECT
'12345.2',
'2018-11-05',
3,
554
FROM system.iota
UNION SELECT
'12346',
'2018-11-05',
5,
678
FROM system.iota
UNION SELECT
'12346.1',
'2018-11-05',
11,
587
FROM system.iota
UNION SELECT
'12346.2',
'2018-11-05',
3,
554
FROM system.iota
;
SELECT
i.orderno,
i.orderdate,
d.orderno,
d.orderdate,
d.itemno
FROM
#ORDERS i,
#ORDERITEM d
WHERE
i.ORDERREADYDATE = '2018-11-07'
AND d.orderdate = i.orderdate
AND d.orderno NOT LIKE '%.%'
;
此SQL会永远运行,如果我删除了d.orderno不喜欢'%。%',它会完成,但我会得到20个重复d.orderno的i.orderno。 ORDERITEM表有超过7密耳记录,每天增长1万。
感谢您的帮助。
答案 0 :(得分:1)
我建议不要使用the old style of join syntax,但是无论您似乎错过了join
条件:i.orderno = d.orderno
Select
i.orderno,
i.orderdate,
d.orderno,
d.orderdate,
d.itemno
from
ORDERS i
join
ORDERITEM d
on
i.orderno = d.orderno
and d.orderdate = i.orderdate
and d.orderno NOT LIKE '%.%'
where
i.ORDERREADYDATE='2018-11-07'
以下是使用旧联接语法的相同查询:
Select
i.orderno,
i.orderdate,
d.orderno,
d.orderdate,
d.itemno
from
#ORDERS i,
#ORDERITEM d
where
i.orderno = d.orderno
and d.orderdate = i.orderdate
and d.orderno NOT LIKE '%.%'
and i.ORDERREADYDATE='2018-11-07'
答案 1 :(得分:0)
我找到了方法,这返回了正确的结果。
Select d.orderno, d.orderdate from ORDERITEMS d
where
d.orderno NOT LIKE '%.%' and d.orderno in
(Select i.orderdate ORDERS i where i.orderreadydate='2018-11-07)