简体   繁体   中英

In clause doesn't work

I have the following query:

SELECT * 
FROM ZINVN 
WHERE ZINVN.GRID IN
(
        SELECT SMGRID 
        FROM SKU FULL JOIN OTBCLS ON SKU.SKCLCD = OTBCLS.SMCLCD
        WHERE SKSKU=800234
);

This query doesn't return a result, while if I run it without the in clause it returns the expected result.

Also the query inside () works well and returns the expected result.

Where is the problem in my query?

You should alias your query to indicate where the columns come from.
Also, an INNER JOIN will be faster than a FULL OUTER JOIN which doesn't quite make sense for two reasons
1. Having the WHERE SKU= clause reduces the FULL JOIN to a LEFT JOIN since rows must match from the left table
2. Keeping records in the LEFT table serves no purpose since SMGRID will be NULL for the purpose of the IN() clause

SELECT * 
FROM ZINVN 
WHERE ZINVN.GRID IN
(
        SELECT OTBCLS.SMGRID 
        FROM SKU JOIN OTBCLS ON SKU.SKCLCD = OTBCLS.SMCLCD
        WHERE SKU.SKSKU=800234
);

This existential test normally executes faster when written in the equivalent form below using a correlated EXISTS test.

SELECT * 
FROM ZINVN 
WHERE EXISTS (
      SELECT 1
      FROM SKU JOIN OTBCLS ON SKU.SKCLCD = OTBCLS.SMCLCD
      WHERE SKU.SKSKU=800234 AND ZINVN.GRID=OTBCLS.SMGRID
      );

Having said that, have you checked for extra spaces being the difference between 'ZINVN.GRID' and 'OTBCLS.SMGRID'?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM