Select Not in Multiple Columns

SELECT NOT IN with multiple columns in subquery

I would recommend not exists:

SELECT *
FROM glsltransaction t
INNER JOIN cocustomer c ON c.customerid = t.acctid
WHERE
??.sltrxstate = 4
AND ??.araccttype = 1
AND NOT EXISTS (
SELECT 1
FROM arapplyitem a
WHERE ??.sltrxid IN (a.ardoccrid, a.ardocdbid)
)

Note that I changed the table aliases to things that are more meaningful. I would strongly recommend prefixing the column names with the table they belong to, so the query is unambiguous - in absence of any indication, I represented this as ?? in the query.

IN sometimes optimize poorly. There are situations where two subqueries are more efficient:

SELECT *
FROM glsltransaction t
INNER JOIN cocustomer c ON c.customerid = t.acctid
WHERE
??.sltrxstate = 4
AND ??.araccttype = 1
AND NOT EXISTS (
SELECT 1
FROM arapplyitem a
WHERE ??.sltrxid = a.ardoccrid
)
AND NOT EXISTS (
SELECT 1
FROM arapplyitem a
WHERE ??.sltrxid = a.ardocdbid
)

Using NOT LIKE on multiple columns

Just use not like:

SELECT *
FROM documents
WHERE (content LIKE '%office%' or notes LIKE '%office%') AND
(content NOT LIKE '%sweden%' and notes NOT LIKE '%sweden%');

Or if you prefer:

SELECT *
FROM documents
WHERE (content LIKE '%office%' or notes LIKE '%office%') AND
NOT (content LIKE '%sweden%' or notes LIKE '%sweden%');

One caveat: NOT LIKE ignores NULL values just as LIKE does. So if the values can be NULL, you need to take that into account.

Can you use multiple columns for a not in query?

It's a SQL extension. Oracle, PostgreSQL and MySQL have it. SQL Server 2005 does not have it. I'm not sure about others.

select rows where not in multiple columns mysql

You can try below -

DEMO

select * from `table` 
where (request_id, p_id) NOT IN ((66, 10),(76,23))

OUTPUT:

request_id  p_id
66 22
66 22
76 24

T-SQL Where not in using two columns

Use a correlated sub-query:

  ... 
WHERE
NOT EXISTS (
SELECT * FROM SecondaryTable WHERE c = FirstTable.a AND d = FirstTable.b
)

Make sure there's a composite index on SecondaryTable over (c, d), unless that table does not contain many rows.

Find a value in one table that exists in multiple columns of another table

You forgot to relate the subquery to your main query. Your query says: "Give me all customers provided there is no row in the accounts table." It should say "Give me all customers for which there is no row in the accounts table."

select cutomername
from customer c
where not exists
(
select null
from accounts a
where c.customername in (a.owner1, a.owner2, a.owner3)
);


Related Topics



Leave a reply



Submit