grouping IN statement sql
lets say I have a table called table1 and it's corresponding columns are col1, col2, col3 and col4 for example.
what will be the equivalent thing of doing:
-- note that the following query will not work
SELECT *
FROM table1
WHERE col1, col2 IN (SELECT col1, col2
FROM table1
WHERE col3 < 4)
Do I have to merge col1 and col2 in my database to make this work? If I merge col1 and col2 into开发者_如何转开发 col1_2 then I will be able to make the above query work by writing:
SELECT *
FROM table1
WHERE col1_2 IN (SELECT col1_2
FROM table1
WHERE col3 < 4)
The IN clause works fine when using one column. it will be nice if I could use it with several columns without having to modify the database.
SELECT * from table1 t1, table1 t2
where t1.col1=t2.col1 and t1.COl2=t2.Col2 and t1.col3<4
try this one
Equivalent of what you showed is:
SELECT *
FROM table1 tb1
WHERE EXISTS
(
SELECT *
FROM table1 tb2
WHERE
tb1.col1 = tb2.col1 and
tb1.col2 = tb2.col2 and
tb2.col3 < 4
)
However, this query does not make much sense as it is equivalent of
SELECT *
FROM table1 tb1
WHERE tb2.col3 < 4
I just assume that the example you show is not well thought out.
精彩评论