mattblack_uk2002 Posted May 2, 2007 Share Posted May 2, 2007 I'm quite new to MySQL and struggling with a MySQL SELECT Statement. I'm working with 3 tables which are as follows: Table 1 User ID Username Table 2 Message ID Author ID (linked to 1.User ID) Subject Message Text Table 3 Message ID Author ID Reciever ID These are obviously not the complete tables however they will suffice to explain my problem. I should let you know that I have to work with these and cannot amend them in any way. I would like to do a SELECT that will give me the author username, reciever username, subject, and text however according to my limited knowledge, this would work out as follows: SELECT Table1.username, Table1.username, Table1.subject etc.... So my question is how can I tell MySQL what to associate with each instance of username? Link to comment https://forums.phpfreaks.com/topic/49645-solved-problem-with-select/ Share on other sites More sharing options...
bubblegum.anarchy Posted May 2, 2007 Share Posted May 2, 2007 Using joins and aliases something like this: SELECT Message_Text , Author.Username , Receiver.Username FROM Table_2 AS INNER JOIN Table_3 ON Table_2.Message_ID = Table_3.Message_ID INNER JOIN Table_1 AS Author ON Table_3.Author_ID = Author.User_ID INNER JOIN Table_1 AS Receiver ON Table_3.Receiver_ID = Receiver.User_ID Link to comment https://forums.phpfreaks.com/topic/49645-solved-problem-with-select/#findComment-243405 Share on other sites More sharing options...
mattblack_uk2002 Posted May 2, 2007 Author Share Posted May 2, 2007 I get 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN Table_3 ON Table_2.Message_ID = Table_3.' at line 1 (I have subsituted the table names for the examples that you supplied) I am using MySQL 4.1... Link to comment https://forums.phpfreaks.com/topic/49645-solved-problem-with-select/#findComment-243452 Share on other sites More sharing options...
paul2463 Posted May 2, 2007 Share Posted May 2, 2007 remove the word AS from the query after table_2 Link to comment https://forums.phpfreaks.com/topic/49645-solved-problem-with-select/#findComment-243459 Share on other sites More sharing options...
mattblack_uk2002 Posted May 2, 2007 Author Share Posted May 2, 2007 Works a treat.... thanks. Link to comment https://forums.phpfreaks.com/topic/49645-solved-problem-with-select/#findComment-243483 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.