Training, Open Source computer languages

This is page http://www.wellho.net/forum/The-MySQ ... abase/How-to-write-an-INNER-JOIN-QUERY-in-Mysql.html

Our email: info@wellho.net • Phone: 01144 1225 708225

How to write an INNER JOIN QUERY in Mysql

Posted by condealmasy (condealmasy), 3 January 2003
Hello.

I have a problem with this Inner Join Query:

SELECT logins.log_log, logins.log_pas, logins.log_cli_id, client_cli_nom, vehicle.veh_pla, vehicle.veh_cli_id, vehicle.veh_sc_ser, sc.sc_ser

FROM logins INNER JOIN (client INNER JOIN (vehicle INNER JOIN sc ON vehicle.veh_sc_ser = sc.sc_ser) ON client.cli_id = vehicle.veh_cli_id) ON logins.log_cli_id = client.cli_id

WHERE ((logins.log_log)=' User1 ') AND ((logins_cliente.log_pas)=' Password 1'));


It returns an an error where the first INNER JOIN starts.

This is how I know how to make those BIG INNER JOINS Queries. I'm new with Mysql so I suppose this type of database works differently with the INNER JOINS.

What I'm trying to do is find the client, vehicle and sc from the logins table, which is the table that relates to the client table, which relates to the vehicle table and so forth.

Any Ideas?

Posted by admin (Graham Ellis), 4 January 2003
Although INNER is a keyword in MySQL, you'll find remarkably little reference to it in the books on MySQL.   This is because (from the MySQL reference manual):
Quote:
INNER JOIN and , are semantically equivalent


Here's an example of a join using the shorter "," notation:
Code:
select books.title, books.isbn, authors.authorlastfirst from authors, books where authors.isbn = books.isbn


In my example, I've only joined two tables, but you could just as well join 3, 4 or more and the syntax is much easier than fighting nested INNER JOINs!

Technical note:  If you join with ",", you use a WHERE clause to specify how the tables are to be joined;  if you omit the where clause, you'll discover that your result set has every record in each incoming table matched to every record in each of the other incoming tables that you specified.  In my sample data set, which has our 343 technical books (including 26 on MySQL!) and 582 authors, that would give me a result set of 199626 records.  



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

© WELL HOUSE CONSULTANTS LTD., 2013: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho