Bizarre SQL select query
Here's something that's been bugging me with SQL recently. Is it possible to create a single query that that can create a list of one non-unique value which *never* has a certain corresponding value in another field? ok.. doesn't make sense... take this table:
Code:
A B Code:
A |
Give me 2 hours, I think I might know how to do it but before posting something that makes me look like an idiot I am going to finish up the code I am writing and test my solution:)
|
select distinct A from table where B='no' and A not in (select distinct A from table where B='yes')
That should be it, but using 'in' can be a little slow.... but I guess if you can make an OUTER JOIN, you could ask it to select A NOT MATCHING As NOT in the other select (A where B='yes'). |
thanks eantoranz, but that's exactly what i don't want.... you are using a subquery, which i am trying to avoid. :)
|
In Oracle the fastest solution is to use minus:
SELECT fld1 from table1 minus SELECT fld1 from table1 where fld2='YES'; This executes fast - the reason is that you MUST do a full-table scan to complete the query anyway. So both 'selects' actually use data from a single full table scan. Sub-queries are not necessarily evil. In this instance (using Oracle) the performance will be the same whether you use the above query or something more complex. You may even degrade performance by using something else. |
How about the outer join?
|
I think you need to use a second query such as:
http://www.mysql.com/doc/en/EXISTS_a...ubqueries.html |
All times are GMT -5. The time now is 03:48 AM. |