如何使用 SQL select 过滤带有类别的行以根据条件加入其他(多个)table?
How to use SQL select to filter rows with category to join other (multiple) table based on condition?
我有以下 4 tables:
**TABLE.INFO**
ID_INFO|INFO_LABLE|CODE_INFO|CATEGORY
2222001|XXXXCCCAA001|88888001|ITEM
2222002|XXXXCCCAA002|88888002|PICKUP
2222003|XXXXCCCAA003|88888002|ITEM
2222004|XXXXCCCAA004|88888001|PICKUP
2222005|XXXXCCCAA005|88888001|PICKUP
2222006|XXXXCCCAA006|88888003|SUPPLIER
**TABLE.ITEM**
ID_ITEM|ID_INFO|DETAIL_ITEM|EXPIRE_DATE
A00001|2222001|COOKIE|5/12/2017
A00002|2222003|PIE|24/11/2017
**TABLE.PICKUP**
ID_PICKUP|ID_INFO|DETAIL_PICKUP|EXPIRE_DATE
P00001|2222004|MOTOR|8/12/2017
P00002|2222005|CAR|26/11/2017
P00003|2222002|TRUCK|2/10/2017
**TABLE.SUPPLIER**
ID_SUPPLIER|ID_INFO|DETAIL_SUPPLIER|EXPIRE_DATE
S00001|2222006|BANANA GREEN|8/12/2017
我想实现选择 TABLE.INFO 上所有字段的结果,条件只有 EXPIRE_DATE < '2017-11-31' 和 CODE_INFO in (88888002,88888001)来自加入其他 table。即
ID_INFO|INFO_LABLE|CODE_INFO|CATEGORY
2222002|XXXXCCCAA002|88888002|PICKUP
2222003|XXXXCCCAA003|88888002|ITEM
2222005|XXXXCCCAA005|88888001|PICKUP
如何使用 join table 和 where 子句实现此目的?我做了如下操作,但没有用。
SELECT A.ID_INFO, A.INFO_LABLE, A.CODE-INFO, A.CATEGORY
FROM TABLE.INFO AS A
JOIN TABLE.ITEM AS B
ON A.ID_INFO = B.ID_INFO
JOIN TABLE.PICKUP AS C
ON A.ID_INFO = C.ID_INFO
JOIN TABLE.SUPPLIER AS D
ON A.ID_INFO = D.ID_INFO
WHERE A.CODE_INFO IN (88888002,88888001) AND B.DATE_EXPIRE < '2017-11-31' OR C.DATE_EXPIRE < '2017-11-31' OR D.DATE_EXPIRE < '2017-11-31';
如果每个table都有一个大记录。对于连接到每个 table 的顺序进程,它会使查询过程时间很长。有没有其他建议,什么时候TABLE.INFO会加入另一个table(TABLE.ITEM,TABLE.PICKUP,TABLE.SUPPLIER)首先根据[中的CATEGORY字段确定=24=]?
试一试:
SELECT A.ID_INFO, A.INFO_LABLE, A.CODE-INFO, A.CATEGORY
FROM TABLE.INFO AS A
INNER JOIN TABLE.ITEM AS B
ON A.ID_INFO = B.ID_INFO AND B.DATE_EXPIRE < '2017-11-31'
WHERE A.CODE_INFO IN (88888002,88888001)
UNION ALL
SELECT A.ID_INFO, A.INFO_LABLE, A.CODE-INFO, A.CATEGORY
FROM TABLE.INFO AS A
INNER JOIN TABLE.PICKUP AS C
ON A.ID_INFO = C.ID_INFO AND C.DATE_EXPIRE < '2017-11-31'
WHERE A.CODE_INFO IN (88888002,88888001)
UNION ALL
SELECT A.ID_INFO, A.INFO_LABLE, A.CODE-INFO, A.CATEGORY
FROM TABLE.INFO AS A
INNER JOIN TABLE.SUPPLIER AS D
ON A.ID_INFO = D.ID_INFO AND D.DATE_EXPIRE < '2017-11-31'
WHERE A.CODE_INFO IN (88888002,88888001)
尝试其中一种方法。
另外,我使用了“2017-12-01”而不是“2017-11-31”。
SELECT info.id_info,
info.info_lable,
info.code_info,
info.category
FROM info
LEFT JOIN item
ON item.id_info = info.id_info
AND item.expire_date < '2017-12-01'
LEFT JOIN pickup
ON pickup.id_info = info.id_info
AND pickup.expire_date < '2017-12-01'
LEFT JOIN supplier
ON supplier.id_info = info.id_info
AND supplier.expire_date < '2017-12-01'
WHERE info.code_info IN ( 88888001, 88888002 )
AND ( item.expire_date IS NOT NULL
OR pickup.expire_date IS NOT NULL
OR supplier.expire_date IS NOT NULL );
SELECT info.id_info, info.info_lable, info.code_info, info.category
FROM info
INNER JOIN item
ON item.id_info = info.id_info AND item.expire_date < '2017-12-01'
WHERE info.code_info IN ( 88888001, 88888002 )
UNION ALL
SELECT info.id_info, info.info_lable, info.code_info, info.category
FROM info
INNER JOIN pickup
ON pickup.id_info = info.id_info AND pickup.expire_date < '2017-12-01'
WHERE info.code_info IN ( 88888001, 88888002 )
UNION ALL
SELECT info.id_info, info.info_lable, info.code_info, info.category
FROM info
INNER JOIN supplier
ON supplier.id_info = info.id_info AND supplier.expire_date < '2017-12-01'
WHERE info.code_info IN ( 88888001, 88888002 );
您可以使用这个查询:
SELECT *
FROM ((SELECT info.*
FROM info
INNER JOIN (SELECT *
FROM item
WHERE expire_date < '2017-11-30') aa
ON info.id_info = aa.id_info)
UNION ALL
(SELECT info.*
FROM info
INNER JOIN (SELECT *
FROM pickup
WHERE expire_date < '2017-11-30') bb
ON info.id_info = bb.id_info)
UNION ALL
(SELECT info.*
FROM info
INNER JOIN (SELECT *
FROM supplier
WHERE expire_date < '2017-11-30') cc
ON info.id_info = cc.id_info))
WHERE code_info IN ( 88888002, 88888001 )
我有以下 4 tables:
**TABLE.INFO**
ID_INFO|INFO_LABLE|CODE_INFO|CATEGORY
2222001|XXXXCCCAA001|88888001|ITEM
2222002|XXXXCCCAA002|88888002|PICKUP
2222003|XXXXCCCAA003|88888002|ITEM
2222004|XXXXCCCAA004|88888001|PICKUP
2222005|XXXXCCCAA005|88888001|PICKUP
2222006|XXXXCCCAA006|88888003|SUPPLIER
**TABLE.ITEM**
ID_ITEM|ID_INFO|DETAIL_ITEM|EXPIRE_DATE
A00001|2222001|COOKIE|5/12/2017
A00002|2222003|PIE|24/11/2017
**TABLE.PICKUP**
ID_PICKUP|ID_INFO|DETAIL_PICKUP|EXPIRE_DATE
P00001|2222004|MOTOR|8/12/2017
P00002|2222005|CAR|26/11/2017
P00003|2222002|TRUCK|2/10/2017
**TABLE.SUPPLIER**
ID_SUPPLIER|ID_INFO|DETAIL_SUPPLIER|EXPIRE_DATE
S00001|2222006|BANANA GREEN|8/12/2017
我想实现选择 TABLE.INFO 上所有字段的结果,条件只有 EXPIRE_DATE < '2017-11-31' 和 CODE_INFO in (88888002,88888001)来自加入其他 table。即
ID_INFO|INFO_LABLE|CODE_INFO|CATEGORY
2222002|XXXXCCCAA002|88888002|PICKUP
2222003|XXXXCCCAA003|88888002|ITEM
2222005|XXXXCCCAA005|88888001|PICKUP
如何使用 join table 和 where 子句实现此目的?我做了如下操作,但没有用。
SELECT A.ID_INFO, A.INFO_LABLE, A.CODE-INFO, A.CATEGORY
FROM TABLE.INFO AS A
JOIN TABLE.ITEM AS B
ON A.ID_INFO = B.ID_INFO
JOIN TABLE.PICKUP AS C
ON A.ID_INFO = C.ID_INFO
JOIN TABLE.SUPPLIER AS D
ON A.ID_INFO = D.ID_INFO
WHERE A.CODE_INFO IN (88888002,88888001) AND B.DATE_EXPIRE < '2017-11-31' OR C.DATE_EXPIRE < '2017-11-31' OR D.DATE_EXPIRE < '2017-11-31';
如果每个table都有一个大记录。对于连接到每个 table 的顺序进程,它会使查询过程时间很长。有没有其他建议,什么时候TABLE.INFO会加入另一个table(TABLE.ITEM,TABLE.PICKUP,TABLE.SUPPLIER)首先根据[中的CATEGORY字段确定=24=]?
试一试:
SELECT A.ID_INFO, A.INFO_LABLE, A.CODE-INFO, A.CATEGORY
FROM TABLE.INFO AS A
INNER JOIN TABLE.ITEM AS B
ON A.ID_INFO = B.ID_INFO AND B.DATE_EXPIRE < '2017-11-31'
WHERE A.CODE_INFO IN (88888002,88888001)
UNION ALL
SELECT A.ID_INFO, A.INFO_LABLE, A.CODE-INFO, A.CATEGORY
FROM TABLE.INFO AS A
INNER JOIN TABLE.PICKUP AS C
ON A.ID_INFO = C.ID_INFO AND C.DATE_EXPIRE < '2017-11-31'
WHERE A.CODE_INFO IN (88888002,88888001)
UNION ALL
SELECT A.ID_INFO, A.INFO_LABLE, A.CODE-INFO, A.CATEGORY
FROM TABLE.INFO AS A
INNER JOIN TABLE.SUPPLIER AS D
ON A.ID_INFO = D.ID_INFO AND D.DATE_EXPIRE < '2017-11-31'
WHERE A.CODE_INFO IN (88888002,88888001)
尝试其中一种方法。
另外,我使用了“2017-12-01”而不是“2017-11-31”。
SELECT info.id_info,
info.info_lable,
info.code_info,
info.category
FROM info
LEFT JOIN item
ON item.id_info = info.id_info
AND item.expire_date < '2017-12-01'
LEFT JOIN pickup
ON pickup.id_info = info.id_info
AND pickup.expire_date < '2017-12-01'
LEFT JOIN supplier
ON supplier.id_info = info.id_info
AND supplier.expire_date < '2017-12-01'
WHERE info.code_info IN ( 88888001, 88888002 )
AND ( item.expire_date IS NOT NULL
OR pickup.expire_date IS NOT NULL
OR supplier.expire_date IS NOT NULL );
SELECT info.id_info, info.info_lable, info.code_info, info.category
FROM info
INNER JOIN item
ON item.id_info = info.id_info AND item.expire_date < '2017-12-01'
WHERE info.code_info IN ( 88888001, 88888002 )
UNION ALL
SELECT info.id_info, info.info_lable, info.code_info, info.category
FROM info
INNER JOIN pickup
ON pickup.id_info = info.id_info AND pickup.expire_date < '2017-12-01'
WHERE info.code_info IN ( 88888001, 88888002 )
UNION ALL
SELECT info.id_info, info.info_lable, info.code_info, info.category
FROM info
INNER JOIN supplier
ON supplier.id_info = info.id_info AND supplier.expire_date < '2017-12-01'
WHERE info.code_info IN ( 88888001, 88888002 );
您可以使用这个查询:
SELECT *
FROM ((SELECT info.*
FROM info
INNER JOIN (SELECT *
FROM item
WHERE expire_date < '2017-11-30') aa
ON info.id_info = aa.id_info)
UNION ALL
(SELECT info.*
FROM info
INNER JOIN (SELECT *
FROM pickup
WHERE expire_date < '2017-11-30') bb
ON info.id_info = bb.id_info)
UNION ALL
(SELECT info.*
FROM info
INNER JOIN (SELECT *
FROM supplier
WHERE expire_date < '2017-11-30') cc
ON info.id_info = cc.id_info))
WHERE code_info IN ( 88888002, 88888001 )