473,583 Members | 2,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AW: How many join do I need for a query on 3 table?

I learned MySQL last year without putting it into action; that is why
I face trouble in formulating my queries. Were it a test, then you
would have passed it, because your queries did help me solve my problem.
I'll turn to MySQL doc after getting through this pressing project.

Thanks a lot Roger!

Babale

-----Urspr=FCngliche Nachricht-----
Von: Roger Baklund [mailto:ro***@ch arlott.no]=20
Gesendet: Samstag, 16. August 2003 14:32
An: my***@lists.mys ql.com
Cc: B. Fongo
Betreff: Re: How many join do I need for a query on 3 table?

* B. Fongo
I was able to extra the information from the first 2 tables yesterday
using following query:

SELECT Customers.Name, Customers.City, Orders.Product,
Order.Price FROM Customers inner join Orders USING (cust_id) WHERE
customers.cust_ id =3D "2"

------------------------------------------------------------------------ --------------------------------------------------------
SELECT Customers.Name, Customers.City, Orders.Product,
Order.Price FROM Customers inner join Orders ON
Customers.cust_ id=3DOrder.cust _id WHERE customers.cust_ id =3D "2"
That was two queries, and they both have a typo preventing them from
working... are you testing us? ;)
Right now I need credit card details from a third table; and that make
the query more complicated for me.
I' m not sure weather 2 inner joins could be used. I' ll appreciate any help.
I can try. :)
I have 2 tables: Customers and orders.
What about the third table you just mentioned...?
The have following structures:
Customers Orders Payment
cust_id Product Order_id
Name Price Credit_Card
City cust_id
Order_id


This was not very readable on my screen...

When you want to show the structure of a table, use "DESC Customers;" or
even better: "SHOW CREATE TABLE Customers;". The last one will also
include
any index definitions, which is often relevant when you ask questions
about
query performance on this list. Just a friendly advice. :)

I think the above means something like this:

Customers:
cust_id INT PRIMARY KEY,
Name VARCHAR,
City VARCHAR
Orders:
Order_id INT PRIMARY KEY,
cust_id INT
Product VARCHAR,
Price INT,
Payment:
Order_id INT,
Credit_Card VARCHAR

(You can only have one product per order, and only full payments are
allowed, and you should record the date of order and date of payment,
but
that's not an issue here, I suppose.)

Ok, prepare for a "5 minute MySQL joining crash course". :)

We will build on your original query, but I will reformat it a little,
just
to make it easier to read. The original, two table query with INNER JOIN
and
ON, reformatted:

SELECT Customers.Name, Customers.City,
Orders.Product, Orders.Price
FROM Customers
INNER JOIN Orders ON
Customers.cust_ id=3DOrders.cus t_id
WHERE
Customers.cust_ id =3D "2"

Then we can expand it with another table:

SELECT Customers.Name, Customers.City,
Orders.Product, Orders.Price,
Payment.Credit_ Card // new line
FROM Customers
INNER JOIN Orders ON
Customers.cust_ id=3DOrders.cus t_id
INNER JOIN Payment ON // new line
Payment.Order_i d=3DOrders.Orde r_id // new line
WHERE
Customers.cust_ id =3D "2"

Now we can simplyfy this by removing some table names we don't need
(because
the column name is unique), and change the ON to USING:

SELECT Name, City, Product, Price, Credit_Card
FROM Customers
INNER JOIN Orders USING(cust_id)
INNER JOIN Payment USING(Order_id)
WHERE
Customers.cust_ id =3D "2"

Note that the order of the tables are important when using USING(): the
previous table is joined with this table USING the named column(s). For
instance, you could not have joined Payment before Orders in the above
statement, because " ... Orders USING(cust_id)" then would have referred
to
the Payment table, which does not have any cust_id column.

We could have used the shortcut alias "," instead of INNER JOIN, but
then we
can not use ON or USING, and must move the join conditions to the WHERE
clause:

SELECT Name, City, Product, Price, Credit_Card
FROM Customers, Orders ,Payment
WHERE
Customers.cust_ id=3DOrders.cus t_id AND
Payment.Order_i d=3DOrders.Orde r_id
Customers.cust_ id =3D "2"

We could also have used NATURAL JOIN in this case, which is the same as
USING naming all columns with the same name in the two joining tables:

SELECT Name, City, Product, Price, Credit_Card
FROM Customers
NATURAL JOIN Orders
NATURAL JOIN Payment
WHERE
Customers.cust_ id =3D "2"

The note about the order of the tables when using USING() also goes for
NATURAL JOIN. Regarding the question in the subject: when joining 'n'
tables, you need 'n-1' JOINS. The JOIN is always placed between two
table
names, and the first table name is always preceeded with "FROM".
Remember
that "," when used between table names in a SELECT is an alias for
"INNER
JOIN".

If you wanted to also include customers which have not payed, you would
use
LEFT JOIN:

SELECT Customers.Name, Customers.City,
Orders.Product, Orders.Price,
IF(Credit_Card, Credit_Card,'*n o pay*') // changed line
FROM Customers
INNER JOIN Orders ON
Customers.cust_ id=3DOrders.cus t_id
LEFT JOIN Payment ON // changed line
Payment.Order_i d=3DOrders.Orde r_id
WHERE
Customers.City =3D "Hamburg" // changed line

....or simply:

SELECT Name, City, Product, Price,
IF(Credit_Card, Credit_Card,'*n o pay*') // changed line
FROM Customers
NATURAL JOIN Orders
NATURAL LEFT JOIN Payment // changed line
WHERE
Customers.City =3D "Hamburg" // changed line

Read more about joining in the manual:

<URL: http://www.mysql.com/doc/en/JOIN.html >

HTH,

--
Roger


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/my***********...ie.nctu.edu.tw

Jul 19 '05 #1
0 3057

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

Similar topics

5
2065
by: jason | last post by:
I could sure use some conceptualization and query help with a Page Watch System I am building in Access 2000 and Asp. I need to cycle through databae and generate a compiliation query email that notifies a person of yacht(s) that have changed on our website: Key database tables...
12
2389
by: jason | last post by:
Access 2000: I have a customer-inventory table I need to loop through and compile a list of all the inventory items the customer is tracking. The problem I am finding is that a simple loop will pull out the customer details each time their is an inventory item listed....I need to get the customer out ONCE and list his items....is there an...
0
1376
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me solve my problem. I'll turn to MySQL doc after getting through this pressing project. Thanks a lot Roger! Babale -----Urspr=FCngliche...
3
12903
by: kj | last post by:
When I run the attached query, I get duplicates when there is one to many relationship between tableA and tableB. The query, tested schema and the result is attached. Sorry for the long post. Here is tested Schema and Data inserts. ---------------------- create table TestTblA (ShipDate datetime, CPEID varchar(30), phonenum char(14))
2
908
by: Keith | last post by:
I am having a problem creating a many-to-many-to-many type relationship. It works fine, but when I create a view to query it and test it, it does not generate the results I expected. Below if the DDL for the tables and the SQL for the view.
1
2600
by: Phil W | last post by:
Hello, I have set up my database in access using many to many relationships (it's the good ol' books and authors one again). I've actually extended it to include other people who contribute in various ways to the books (e.g. editors, illustrators etc), so there's more than one many to many relationship there. When there's an entry in all...
4
2065
by: Apple | last post by:
I have to create a query with many to many relationship, but I can't break it into 2 x 1 to many, should there anyone can teach me how to solve this problem. Thanks in advance!
5
1722
by: Jean Christophe Avard | last post by:
Hi! I have a query that INNER JOIN's three other tables. All the relation are 1 to 1, except for one, where an Item can have more than one price. I have this query that work fine in MSDE Query Analyzer, but when I fill a dataset with it, I get an error. (I attached a screenshot of it) visual basic...
2
6414
by: Neekos | last post by:
Im working on web tool that will allow supervisors to see a list of their agents and their call stats for a call center. I have one main table that holds employee IDs and their supervisor names. I then have a dozen other tables that hold many different employee statistics and scores that they accumulate through the year. These tables all have...
0
7890
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7815
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7926
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...
0
8187
tracyyun
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...
0
6573
agi2029
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...
1
5691
isladogs
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...
0
5367
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1418
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.