Jump to content

Recommended Posts

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

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

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...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.