473,748 Members | 10,649 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 3069

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

Similar topics

5
2080
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 ---------------------------------------------------------------------------- Customer (1) --->> (many) Customer_Boats (many)<<---- Boat (1)
12
2405
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 elegant way to do this: Here is the table: Cust-Invent Table
0
1389
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 Nachricht-----
3
12915
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
2610
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 the tables relating to the book I want to view, everything works fine (i.e. there's an author in...
4
2073
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
1727
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 code:------------------------------------------------------------------------------
2
6422
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 employee IDs in them, but the IDs would reoccur everyday (so they are not unique) and there is data...
0
8830
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9541
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9247
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
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
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
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...

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.