Multiple Joins (SQL)

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Multiple Joins (SQL)

Post by Cruzado_Mainfrm »

How do you make multiple joins? i know how to make a join of two tables, for ex:

Code: Select all

<?php
$query = "SELECT l.lyricid,l.hits,t.titlename FROM lyrichits AS l LEFT JOIN titles AS t ON (l.lyricid=t.lyricid) ORDER BY hits DESC LIMIT 0,10";
?>
but i wanna make a join that resembles this

tables:
-<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> //visits to every lyric. fields: lyricid(relation),hits.
-titles //contains all the titles. fields: titleid,lyricid(relation),titlename,artistid(relation)
-artists //contains the name of the artists. fields: artistid(relation),artistname
-lyrics //table not used. fields: lyricid(relation),data

and the sql query has to return the top 10 lyrics with this data:
artistid,artistname,lyricid,titlename

and for this i need two joins instead of one, a thing that i don't know how to do, any help :D?
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

LOL, solved:

Code: Select all

<?php
$query = "SELECT l.lyricid,l.hits,t.titlename,a.artistname FROM lyrichits AS l LEFT JOIN titles AS t USING (lyricid) LEFT JOIN artists AS a USING(artistid) ORDER BY hits DESC LIMIT 0,10";
?>
Post Reply