Segbarn Posted January 13, 2007 Share Posted January 13, 2007 Hello I am very new to MySQL and PHP but I think I am mature enough to be able to post here.I have 3 tables where I want to list all rows of the table "nuke_demands".I want to have the 8 columns username,from_servername,from_servercountry,from_servertype,to_servername,to_servercountry,to_servertype,amount so that I can use "ORDER BY" statement properly.I dont understand how to handle a join when there are 3 tables involved. If I divide the queries in two I cannot do the ORDER thing right? Can you please give me a hint?username is in nuke_users and should be getable through the user_id.the serverinfo should be some kind of join of nuke_servers and nuke_demands.amount is just picked from nuke_demands directly.table: nuke_usersuser_id usernametable: nuke_serversid servername country typetable: nuke_demandsuser_id from_server to_server amount[i]from_server and to_server are the ids of servers[/i]This is what I tried:[code]sql_test3 = "SELECT username,from_servername,from_servercountry,from_servertype,to_servername,to_servercountry,to_servertype,amount FROM nuke_users,nuke_servers WHERE username in (SELECT nuke_users.username FROM nuke_demands,nuke_usersWHERE (nuke_demands.user_id=nuke_users.user_id)) AND (from_servername,from_servercountry,from_servertype) in (SELECT nuke_servers.servername,nuke_servers.country,nuke_servers.type FROM nuke_demands,nuke_servers WHERE nuke_demands.from_server_id=nuke_servers.id) AND (to_servername,to_servercountry,to_servertype) in (SELECT nuke_servers.servername,nuke_servers.country,nuke_servers.type FROM nuke_demands,nuke_servers WHERE nuke_demands.to_server_id=nuke_servers.id) AND amount in (SELECT amount FROM nuke_demands)";[/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted January 14, 2007 Share Posted January 14, 2007 try[code]SELECT u.username, d.amount, sf.servername as from_servername, sf.country as from_servercountry, sf.type as from_servertype, st.servername as to_servername, st.country as to_servercountry, st.type as to_servertypeFROM nuke_users u INNER JOIN nuke_demands d ON u.user_id = d.user_id INNER JOIN nuke_servers sf ON sf.id = d.from_server INNER JOIN nuke_servers st ON st.id = d.to_serverORDER BY u.username, sf.servername, st.servername[/code]Note the join to nuke_servers twice - to pick up name,country,type for the from_server and again for the to_server Quote Link to comment Share on other sites More sharing options...
Segbarn Posted January 14, 2007 Author Share Posted January 14, 2007 Worked like a charm! I am sooo happy, thanks alot! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.