473,388 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Negative SELECT in mysql?

How does one retrieve the rows in a select statement that DONT match the
select.

select CarIndex FROM DealerCatalog, BigCatalog WHERE
DealerCatalog.CarIndex=BigCatalog.CarIndex

finds all the cars in the dealer catalog that are in the bigger distributor
catalog.

How do I do the opposite in a single sql statement i.e. all the dealer cars
that AREN'T in the big distributor catalog?

Is there a negative Select?

Jul 17 '05 #1
12 14037
kaptain kernel wrote:

How does one retrieve the rows in a select statement that DONT match the
select.

select CarIndex FROM DealerCatalog, BigCatalog WHERE
DealerCatalog.CarIndex=BigCatalog.CarIndex

finds all the cars in the dealer catalog that are in the bigger distributor
catalog.

How do I do the opposite in a single sql statement i.e. all the dealer cars
that AREN'T in the big distributor catalog?

Is there a negative Select?


select CarIndex FROM DealerCatalog, BigCatalog WHERE
DealerCatalog.CarIndex<>BigCatalog.CarIndex

Regards,
Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com

I have a spam filter. Please include "PHP" in the
subject line to ensure I'll get your message.
Jul 17 '05 #2
On 2004-01-13, kaptain kernel <no****@nospam.gov> wrote:
How does one retrieve the rows in a select statement that DONT match the
select.

select CarIndex FROM DealerCatalog, BigCatalog WHERE
DealerCatalog.CarIndex=BigCatalog.CarIndex

finds all the cars in the dealer catalog that are in the bigger distributor
catalog.

How do I do the opposite in a single sql statement i.e. all the dealer cars
that AREN'T in the big distributor catalog?

Is there a negative Select?


Lookup the section about the WHERE clause in your database manual.

In this case it's enough to reverse the condition.
More general you might use the not in with subquery construct.

--
http://home.mysth.be/~timvw
Jul 17 '05 #3
Tim Van Wassenhove wrote:
On 2004-01-13, kaptain kernel <no****@nospam.gov> wrote:
How does one retrieve the rows in a select statement that DONT match the
select.

select CarIndex FROM DealerCatalog, BigCatalog WHERE
DealerCatalog.CarIndex=BigCatalog.CarIndex

finds all the cars in the dealer catalog that are in the bigger distributor
catalog.

How do I do the opposite in a single sql statement i.e. all the dealer cars
that AREN'T in the big distributor catalog?

Is there a negative Select?

Lookup the section about the WHERE clause in your database manual.

In this case it's enough to reverse the condition.
More general you might use the not in with subquery construct.

The suggestions mentioned above are certainly not speedy - I had to kill
a sql query when i changed the = in my original statement to <> as
suggested above. Which tells me that the suggestions are wrong.

The answer is to use LEFT JOIN - anything that doesn't join is given a
NULL value , and it's a heck of a lot speedier of large datasets (i've
got 12,000 records):

SELECT CarIndex FROM DealerCatalog LEFT JOIN BigCatalog ON
DealerCatalog.CarIndex=BigCatalog.CarIndex WHERE BigCatalog.CarIndex IS NULL

Jul 17 '05 #4
kaptain kernel wrote:
Tim Van Wassenhove wrote:
On 2004-01-13, kaptain kernel <no****@nospam.gov> wrote:
How does one retrieve the rows in a select statement that DONT
match the select.

select CarIndex FROM DealerCatalog, BigCatalog WHERE
DealerCatalog.CarIndex=BigCatalog.CarIndex

finds all the cars in the dealer catalog that are in the bigger
distributor catalog.

How do I do the opposite in a single sql statement i.e. all the
dealer cars that AREN'T in the big distributor catalog?

Is there a negative Select?

Lookup the section about the WHERE clause in your database manual.

In this case it's enough to reverse the condition.
More general you might use the not in with subquery construct.

The suggestions mentioned above are certainly not speedy - I had to
kill a sql query when i changed the = in my original statement to
<> as
suggested above. Which tells me that the suggestions are wrong.


No kidding. You asked for a cartesian product by specifying a join of two
tables with essentially no join condition...

The answer is to use LEFT JOIN - anything that doesn't join is given a
NULL value , and it's a heck of a lot speedier of large datasets (i've
got 12,000 records):

SELECT CarIndex FROM DealerCatalog LEFT JOIN BigCatalog ON
DealerCatalog.CarIndex=BigCatalog.CarIndex WHERE BigCatalog.CarIndex
IS NULL


Actually, this is definitely not the optimal way to do it, as you are doing
a join but not using any information from the second relation. This is going
to be rather slow. Instead, do:

SELECT CarIndex FROM DealerCatalog WHERE DealerCatalog.CarIndex NOT IN
(SELECT CarIndex FROM BigCatalog);

Additionally, you should make sure that there is an index on
DealerCatalog.CarIndex and also an index on BigCatalog.CarIndex.

-Ian
Jul 17 '05 #5
or

SELECT CarIndex FROM DealerCatalog WHERE NOT EXISTS
(SELECT * FROM BigCatalog WHERE DealerCatalog.CarIndex =
BigCatalog.CarIndex);

Uzytkownik "Agelmar" <if**********@comcast.net> napisal w wiadomosci
news:bu************@ID-30799.news.uni-berlin.de...
Actually, this is definitely not the optimal way to do it, as you are doing a join but not using any information from the second relation. This is going to be rather slow. Instead, do:

SELECT CarIndex FROM DealerCatalog WHERE DealerCatalog.CarIndex NOT IN
(SELECT CarIndex FROM BigCatalog);

Additionally, you should make sure that there is an index on
DealerCatalog.CarIndex and also an index on BigCatalog.CarIndex.

-Ian

Jul 17 '05 #6
Chung Leong wrote:
or

SELECT CarIndex FROM DealerCatalog WHERE NOT EXISTS
(SELECT * FROM BigCatalog WHERE DealerCatalog.CarIndex =
BigCatalog.CarIndex);


No, that is much slower. Your method will result in a sub-query on the
database for each CarIndex in DealerCatalog. My method results in the
subquery being evaluated only once, as it is not a correlated subquery. The
database retrieves a list of CarIndex tuples from the BigCatalog relation,
and it does this only once, storing this in memory. It then probes this list
for each CarIndex in DealerCatalog. With your method, it takes CarIndex
values from DealerCatalog one at a time, and for each such value, it issues
a query against BigCatalog. Not ideal.
Jul 17 '05 #7
kaptain kernel wrote:
Tim Van Wassenhove wrote:
On 2004-01-13, kaptain kernel <no****@nospam.gov> wrote:
How does one retrieve the rows in a select statement that DONT match the
select.
[snip[
How do I do the opposite in a single sql statement i.e. all the
dealer cars
that AREN'T in the big distributor catalog?

Is there a negative Select?

[snip]
The answer is to use LEFT JOIN - anything that doesn't join is given a
NULL value , and it's a heck of a lot speedier of large datasets (i've
got 12,000 records):

SELECT CarIndex FROM DealerCatalog LEFT JOIN BigCatalog ON
DealerCatalog.CarIndex=BigCatalog.CarIndex WHERE BigCatalog.CarIndex IS
NULL


If your RDBMS vendor implemented the SET operators using the
MINUS operator may prove to be much faster; e.g.

SELECT CarIndex FROM DealerCatalog
MINUS
SELECT CarIndex FROM BigCatalog
ORDER BY 1;

Jul 17 '05 #8
kaptain kernel wrote:

Tim Van Wassenhove wrote:
On 2004-01-13, kaptain kernel <no****@nospam.gov> wrote:
How does one retrieve the rows in a select statement that DONT match the
select.

select CarIndex FROM DealerCatalog, BigCatalog WHERE
DealerCatalog.CarIndex=BigCatalog.CarIndex

finds all the cars in the dealer catalog that are in the bigger distributor
catalog.

How do I do the opposite in a single sql statement i.e. all the dealer cars
that AREN'T in the big distributor catalog?

Is there a negative Select?

Lookup the section about the WHERE clause in your database manual.

In this case it's enough to reverse the condition.
More general you might use the not in with subquery construct.


The suggestions mentioned above are certainly not speedy - I had to kill
a sql query when i changed the = in my original statement to <> as
suggested above. Which tells me that the suggestions are wrong.

The answer is to use LEFT JOIN - anything that doesn't join is given a
NULL value , and it's a heck of a lot speedier of large datasets (i've
got 12,000 records):

SELECT CarIndex FROM DealerCatalog LEFT JOIN BigCatalog ON
DealerCatalog.CarIndex=BigCatalog.CarIndex WHERE BigCatalog.CarIndex IS NULL


My apologies - I didn't notice there 2 tables. I was thinking of just a select
from a single table.

Regards,
Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com

I have a spam filter. Please include "PHP" in the
subject line to ensure I'll get your message.
Jul 17 '05 #9
Depends on the database software. On MS SQLServer using EXISTS is much
faster than using IN, rather counterintuitively.

Uzytkownik "Agelmar" <if**********@comcast.net> napisal w wiadomosci
news:bu************@ID-30799.news.uni-berlin.de...
Chung Leong wrote:
or

SELECT CarIndex FROM DealerCatalog WHERE NOT EXISTS
(SELECT * FROM BigCatalog WHERE DealerCatalog.CarIndex =
BigCatalog.CarIndex);
No, that is much slower. Your method will result in a sub-query on the
database for each CarIndex in DealerCatalog. My method results in the
subquery being evaluated only once, as it is not a correlated subquery.

The database retrieves a list of CarIndex tuples from the BigCatalog relation,
and it does this only once, storing this in memory. It then probes this list for each CarIndex in DealerCatalog. With your method, it takes CarIndex
values from DealerCatalog one at a time, and for each such value, it issues a query against BigCatalog. Not ideal.

Jul 17 '05 #10
Chung Leong wrote:
Depends on the database software. On MS SQLServer using EXISTS is much
faster than using IN, rather counterintuitively.


I wouldn't be suprised. SQL Server has a very powerful, rule-based query
optimizer. MySQL's query optimizer is far less powerful (it's quite minimal
compared to Oracle, DB2, SQL Server...), and so I'm pretty sure that it will
run the IN faster than the NOT EXISTS... but I am too lazy to make a large
data set to actually test it out :-)
Jul 17 '05 #11
I didn't even know that MySQL can handle subqueries.

Uzytkownik "Agelmar" <if**********@comcast.net> napisal w wiadomosci
news:bu************@ID-30799.news.uni-berlin.de...
Chung Leong wrote:
Depends on the database software. On MS SQLServer using EXISTS is much
faster than using IN, rather counterintuitively.
I wouldn't be suprised. SQL Server has a very powerful, rule-based query
optimizer. MySQL's query optimizer is far less powerful (it's quite

minimal compared to Oracle, DB2, SQL Server...), and so I'm pretty sure that it will run the IN faster than the NOT EXISTS... but I am too lazy to make a large
data set to actually test it out :-)

Jul 17 '05 #12
Only the latest (or next versions) can...

(I can't remember if it is the current release, or
the beta for the next one).

--
Dag.

"Chung Leong" <ch***********@hotmail.com> wrote in message
news:LO********************@comcast.com...
I didn't even know that MySQL can handle subqueries.

Uzytkownik "Agelmar" <if**********@comcast.net> napisal w wiadomosci
news:bu************@ID-30799.news.uni-berlin.de...
Chung Leong wrote:
Depends on the database software. On MS SQLServer using EXISTS is much
faster than using IN, rather counterintuitively.


I wouldn't be suprised. SQL Server has a very powerful, rule-based query
optimizer. MySQL's query optimizer is far less powerful (it's quite

minimal
compared to Oracle, DB2, SQL Server...), and so I'm pretty sure that it

will
run the IN faster than the NOT EXISTS... but I am too lazy to make a large data set to actually test it out :-)


Jul 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Rick | last post by:
I need to store a date prior to the unix epoch, any pointers? -- Rick Digital Printing www.intelligence-direct.com
21
by: John Fabiani | last post by:
Hi, I'm a newbie and I'm attempting to learn howto create a select statement. When I use >>> string1='18 Tadlock Place' >>> cursor.execute("SELECT * FROM mytest where address = %s",string1) All...
0
by: Fatt Shin | last post by:
Hi, I'm running MySQL 4.0.13, connecting from PowerBuilder 9 using ODCB Connector 3.51. I'm facing a problem where whenever I issue a SELECT COUNT(*) statement from PowerBuilder, I always get SQL...
0
by: Hans Maurer | last post by:
>Description: We're running our current TTS application with MySQL (on Unix). All database, table and column names are in lower-case. However, we need to access this database with a new...
2
by: Richard van Denzel | last post by:
Hi All, I've defined a table (m) with an double field (bedrag) in it and when I execute the next statement: INSERT INTO m (bedrag) VALUES('-7000,00'); the value get stored correctly. ...
5
by: jayson_13 | last post by:
Hi, I need to implement a counter and i face problem of locking so hope that u guys can help me. I try to do test like this : 1st connection SELECT * FROM nextkey WHERE tblname = 'PLCN'...
7
by: pj | last post by:
Why does M$ Query Analyzer display all numbers as positive, no matter whether they are truly positive or negative ? I am having to cast each column to varchar to find out if there are any...
3
by: Hendry Taylor | last post by:
I have a problem where if I issue a select * from against a database it returns no data, but if I select column from it returns the data. Why would the * not be working as a wildcard?
3
by: abighill | last post by:
Problem ------------- I want to return all URL records from 'fett_url' that are not currently indexed in the lookup table 'fett_url_to_data' where 'data_id=2'. i.e. fields=> url_id,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.