I have three tables, containing information about three people and the
cars that they own.
One table lists people, one lists cars, and one uses the id's from the
other two to compile a list of which models of car each person owns.
Bob owns a Ford, Paul owns a Toyota, and Dave owns both a Ford and a
Toyota.
Simple.
How do I create a query that will tell me 'which people own a Ford and
do not own a Toyota' - i.e. how do you get a result of just Bob?
table: person
fields: person_id, person_name
data: 1 - Bob, 2 - Paul, 3 - Dave
table: car
fields: car_id, car_name
data: 1 - Ford, 2 - Toyota
table: person_car
fields: person_id, car_id
data: 1 - 1, 2 - 2, 3 - 1, 3 - 2 10 2112
This lists which people own a only a Ford,
SELECT P.person_name FROM person P,car C, person_car PC
WHERE PC. person_id=P.per son_id and PC.car_id=C.car _id and
C.car_name='For d';
This lists which people own a only a Ford,
SELECT P.person_name,c ount(PC.car_id) MyNo FROM person P,car C,
person_car PC
WHERE PC. person_id=P.per son_id and PC.car_id=C.car _id and
C.car_name='For d' and MyNo=1 m.******@btinte rnet.com wrote: I have three tables, containing information about three people and the cars that they own. One table lists people, one lists cars, and one uses the id's from the other two to compile a list of which models of car each person owns. Bob owns a Ford, Paul owns a Toyota, and Dave owns both a Ford and a Toyota.
Simple.
How do I create a query that will tell me 'which people own a Ford and do not own a Toyota' - i.e. how do you get a result of just Bob?
table: person fields: person_id, person_name data: 1 - Bob, 2 - Paul, 3 - Dave
table: car fields: car_id, car_name data: 1 - Ford, 2 - Toyota
table: person_car fields: person_id, car_id data: 1 - 1, 2 - 2, 3 - 1, 3 - 2
select person.person_n ame from person inner join person_car on
person.person_i d = person_car.pers on_id inner join car on car.car_id =
person_car.car_ id where car.car_name='f ord' and car.car_name!=' toyota';
--
visit: http://www.h4xx0r.ch
Did the other solutions work? They don't.. Here's how I would do it:
SELECT p.Person_Name
FROM PERSON p
INNER JOIN
(SELECT pc.Person_ID, pc.Car_ID,c.Car _Name
FROM PERSON_CAR pc
INNER JOIN CAR c
ON c.Car_ID = pc.Car_ID
WHERE c.Car_Name = 'FORD') ford
ON ford.Person_ID = p.Person_ID
LEFT JOIN
(SELECT pc.Person_ID, pc.car_ID,c.Car _Name
FROM PERSON_CAR pc
INNER JOIN CAR c
ON c.Car_ID = pc.Car_ID
WHERE c.Car_Name = 'Toyota') toyota
ON toyota.Person_I D = p.Person_ID
WHERE toyota.Car_ID IS NULL
<m.******@btint ernet.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com... I have three tables, containing information about three people and the cars that they own. One table lists people, one lists cars, and one uses the id's from the other two to compile a list of which models of car each person owns. Bob owns a Ford, Paul owns a Toyota, and Dave owns both a Ford and a Toyota.
Simple.
How do I create a query that will tell me 'which people own a Ford and do not own a Toyota' - i.e. how do you get a result of just Bob?
table: person fields: person_id, person_name data: 1 - Bob, 2 - Paul, 3 - Dave
table: car fields: car_id, car_name data: 1 - Ford, 2 - Toyota
table: person_car fields: person_id, car_id data: 1 - 1, 2 - 2, 3 - 1, 3 - 2
"Rich Ryan" <ry****@sbcglob al.net> wrote in message
news:9y******** ******@newssvr3 0.news.prodigy. com... Did the other solutions work? They don't.. Here's how I would do it:
I agree, the other solutions proposed fail in certain cases.
Here's another solution similar to Rich's:
SELECT DISTINCT p.*
FROM person AS p
INNER JOIN person_car AS pc ON p.person_id = pc.person_id
INNER JOIN car AS c ON pc.car_id = c.car_id AND c.car_name = 'Ford'
LEFT OUTER JOIN person_car AS pc2 ON p.person_id = pc2.person_id
INNER JOIN car AS c2 on pc2.car_id = c2.car_id AND c2.car_name = 'Toyota'
WHERE c2.car_id IS NULL;
Regards,
Bill K.
This is the nested query solution that I originally came up with. I'm
concerned that using a nested query would mean more work for the
database, so that's my reason for wanting a solution that just uses
joins. My knowledge of such things is limited - am I right to assume
that a nested query will automatically use more resources to process
than a regular non-nested query?
Michael
SELECT p.person_name
FROM person p, person_car pc
WHERE pc.car_id =1
AND p.person_id = pc.person_id
AND pc.person_id NOT
IN (
SELECT pc.person_id
FROM person_car pc
WHERE pc.car_id =2
)
CREATE TABLE `car` (
`car_id` mediumint(9) NOT NULL auto_increment,
`car_name` varchar(20) NOT NULL default '',
PRIMARY KEY (`car_id`)
);
INSERT INTO `car` VALUES (1, 'Ford');
INSERT INTO `car` VALUES (2, 'Toyota');
CREATE TABLE `person` (
`person_id` mediumint(9) NOT NULL auto_increment,
`person_name` varchar(20) NOT NULL default '',
PRIMARY KEY (`person_id`)
);
INSERT INTO `person` VALUES (1, 'Bob');
INSERT INTO `person` VALUES (2, 'Paul');
INSERT INTO `person` VALUES (3, 'Dave');
CREATE TABLE `person_car` (
`pc_id` mediumint(9) NOT NULL auto_increment,
`person_id` mediumint(9) NOT NULL default '0',
`car_id` mediumint(9) NOT NULL default '0',
PRIMARY KEY (`pc_id`)
);
INSERT INTO `person_car` VALUES (1, 1, 1);
INSERT INTO `person_car` VALUES (2, 2, 2);
INSERT INTO `person_car` VALUES (3, 3, 1);
INSERT INTO `person_car` VALUES (4, 3, 2);
Does the solution run too slow? One way or another you're going to need
tables that contain ownership information. There is no other way to solve
this problem. So you can create intermediate tables or just do the same
thing in your SELECT.
My approach is to write a query in the "most SQL" way, so that others can
understand what's going on. If performance becomes an issue, then I do an
"EXPLAIN" to see just what's going on.
Remember, SQL is a set-based (really bag-based) language, so you want to use
it properly.
I'd like to see your original "nested query"
Rich
<m.******@btint ernet.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. . This is the nested query solution that I originally came up with. I'm concerned that using a nested query would mean more work for the database, so that's my reason for wanting a solution that just uses joins. My knowledge of such things is limited - am I right to assume that a nested query will automatically use more resources to process than a regular non-nested query?
Michael
SELECT p.person_name FROM person p, person_car pc WHERE pc.car_id =1 AND p.person_id = pc.person_id AND pc.person_id NOT IN ( SELECT pc.person_id FROM person_car pc WHERE pc.car_id =2 )
CREATE TABLE `car` ( `car_id` mediumint(9) NOT NULL auto_increment, `car_name` varchar(20) NOT NULL default '', PRIMARY KEY (`car_id`) );
INSERT INTO `car` VALUES (1, 'Ford'); INSERT INTO `car` VALUES (2, 'Toyota');
CREATE TABLE `person` ( `person_id` mediumint(9) NOT NULL auto_increment, `person_name` varchar(20) NOT NULL default '', PRIMARY KEY (`person_id`) );
INSERT INTO `person` VALUES (1, 'Bob'); INSERT INTO `person` VALUES (2, 'Paul'); INSERT INTO `person` VALUES (3, 'Dave');
CREATE TABLE `person_car` ( `pc_id` mediumint(9) NOT NULL auto_increment, `person_id` mediumint(9) NOT NULL default '0', `car_id` mediumint(9) NOT NULL default '0', PRIMARY KEY (`pc_id`) );
INSERT INTO `person_car` VALUES (1, 1, 1); INSERT INTO `person_car` VALUES (2, 2, 2); INSERT INTO `person_car` VALUES (3, 3, 1); INSERT INTO `person_car` VALUES (4, 3, 2);
Sorry, you did post your solution. Just didn't scroll down.
Rich
<m.******@btint ernet.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. . This is the nested query solution that I originally came up with. I'm concerned that using a nested query would mean more work for the database, so that's my reason for wanting a solution that just uses joins. My knowledge of such things is limited - am I right to assume that a nested query will automatically use more resources to process than a regular non-nested query?
Michael
SELECT p.person_name FROM person p, person_car pc WHERE pc.car_id =1 AND p.person_id = pc.person_id AND pc.person_id NOT IN ( SELECT pc.person_id FROM person_car pc WHERE pc.car_id =2 )
CREATE TABLE `car` ( `car_id` mediumint(9) NOT NULL auto_increment, `car_name` varchar(20) NOT NULL default '', PRIMARY KEY (`car_id`) );
INSERT INTO `car` VALUES (1, 'Ford'); INSERT INTO `car` VALUES (2, 'Toyota');
CREATE TABLE `person` ( `person_id` mediumint(9) NOT NULL auto_increment, `person_name` varchar(20) NOT NULL default '', PRIMARY KEY (`person_id`) );
INSERT INTO `person` VALUES (1, 'Bob'); INSERT INTO `person` VALUES (2, 'Paul'); INSERT INTO `person` VALUES (3, 'Dave');
CREATE TABLE `person_car` ( `pc_id` mediumint(9) NOT NULL auto_increment, `person_id` mediumint(9) NOT NULL default '0', `car_id` mediumint(9) NOT NULL default '0', PRIMARY KEY (`pc_id`) );
INSERT INTO `person_car` VALUES (1, 1, 1); INSERT INTO `person_car` VALUES (2, 2, 2); INSERT INTO `person_car` VALUES (3, 3, 1); INSERT INTO `person_car` VALUES (4, 3, 2);
<m.******@btint ernet.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. . This is the nested query solution that I originally came up with. I'm concerned that using a nested query would mean more work for the database, so that's my reason for wanting a solution that just uses joins. My knowledge of such things is limited - am I right to assume that a nested query will automatically use more resources to process than a regular non-nested query?
Not necessarily. In this case, the subquery is invariant with respect to
the rows of the outer query. The MySQL optimizer is smart enough (and this
is true for any reasonable RDBMS product) to perform an invariant subquery
once, and use the results as though it were a list of constant values, to
evaluate all the rows of the outer query.
I predict that the performance difference, whether positive or negative,
between this and getting the same results with a join, will be negligible.
But as always in these sorts of issues, both your assumption and mine need
to be measured for us to be sure. Do both types of queries, and time them.
Your particular database schema and data content, MySQL version, and even
your server host can potentially affect the results to some degree, so
there's no substitute for running tests with your data.
Regards,
Bill K. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: BP |
last post by:
I get the following error: Error Type:
Microsoft VBScript runtime (0x800A01A8)
Object required: ''
/myweb4/authorised_user_page.asp, line 70
and line 70 of my code is: <% rsCheckUser1.Open strSQL1,
strcon1 %>
Here is the full code:
|
by: Shane Niebergall |
last post by:
Can someone explain why there is such a big performance difference in these
two queries? Note: I have a multicolumn index on ltid and inuse.
mysql> UPDATE leads SET inuse='0' WHERE inuse!='0' and ltid='8' and
inuse<'1098298863';
Query OK, 0 rows affected (46.35 sec)
Rows matched: 0 Changed: 0 Warnings: 0
mysql> UPDATE leads SET inuse='0' WHERE inuse='1' and ltid='8' and
inuse<'1098298863';
|
by: Robin Tucker |
last post by:
I have these 3 queries - they are the same except each fetches record counts
for one of 3 different record types, nSubsets (type 0), nAssets (type 1) and
nImages (type 2). Is there any way I could get all 3 of these (based on the
Node.Type integer) with a single query?
IF @Error = 0
BEGIN
SELECT @nSubsets = COUNT(*)
FROM Node
INNER JOIN Adjacency
|
by: chudson007 |
last post by:
I have a table, TableA with amongst other fields, a field for Qty.
Qty can range from 0 to 100.
How do I count the number of rows with a qty between 1 and 10, 11 and
20, 21 and 30, and so on using one SQL statement?
Regards,
Ciarán
|
by: My SQL |
last post by:
Hi,
I started the general query log with
mysqld --log
On my machine this command works fine and every select, update and all
statements are logged. I tried the same command on another machine and
many many queries are missing, I am running apache, PHP on that machine
and all the queries come through the web on localhost.
| |
by: marko |
last post by:
Hy!
I have 4 queries which have 3 fields which have the same names. How can
I make a query which contains all the data from the other 4 queries?
Thanks,
Marko
|
by: Prashanth Badabagni |
last post by:
Hi,
Can any one please get me all the queries in the comp.lang.c in a
..ZIP file or atleast some of the queries ...
Thanks in Advance
|
by: Tingo |
last post by:
Hi all,
Is it possible to create a message queue with a specific ID? I want to
do this because I'm trying to write a piece of software which restores
communicating processes (which communicate through message queues) when
there is a machine failure. When restarting the machine I need to setup
the message queues as they were originally.
I find that given the same key, when creating a queue, the queue ID is
not always the same value. Is...
|
by: BerkshireGuy |
last post by:
I am using Windows 2000 and Access 2003 and noticed that when I click a
control on a report, it takes a few seconds for the "click" to take
place. Its laggy. Same with moving controls, adding new ones, etc.
Any ideas?
Users in my company are reporting slowness in MS Word as well.
We also have Lotus Notes.
|
by: jai83 |
last post by:
hi,
How can Stored procedures be used in oracle reports for creating reports instead of SQL queries?
Thank you.
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |