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
How to Save the Result of a SQL Query into a Variable in Vba
How to Count Decimal Places in SQL
Using an Alias Column in the Where Clause in Ms-SQL 2000
Set Limit for a Table Rows in SQL
SQL Server Equivalent to MySQL's Explain
Oracle Date To_Char('Month Dd, Yyyy') Has Extra Spaces in It
Cannot Select from Update Returning Clause in Postgres
Referencing Current Row in Filter Clause of Window Function
Insert Data and Set Foreign Keys with Postgres
Subtract Hours from the Now() Function
Where Does the Practice "Exists (Select 1 from ...)" Come From
SQL Server, Can't Insert Null into Primary Key Field
How to Read Multiple Result Sets Returned from a SQL Server Stored Procedure in R
Translating SQL Joins on Foreign Keys to R Data.Table Syntax
Work Around SQL Server Maximum Columns Limit 1024 and 8Kb Record Size