Skip to content
Advertisement

MySQL Query Help for Multiple Tables

Alright so I’m working on a final assignment and I’m super stuck on this one task. I’ve tried looking it up and reading guides and I got nowhere. To make matters worse, my professor hasn’t bothered to teach us how to do this particular type of task and I can’t bother asking him for help since it takes him days to respond. The task is,

“Find All of the Messages that Michael Phelps Sent.

Construct the SQL statement to find all of the messages that Michael Phelps sent.

Note: You must use the WHERE clause to set the conditions for this query.”

Display the following columns:

  • Sender’s first name
  • Sender’s last name
  • Receiver’s first name
  • Receiver’s last name
  • Message ID
  • Message
  • Message Timestamp

I have 5 tables in my database (contact_list, image, message_image, person, and message).

Generally this would be fairly easy, however, I’m stuck on how to get the sender’s and receiver’s names. I have to somehow cross reference the sender_id and receiver_id from the message table with the person_id from the person table. That is what I’m stuck on.

How the heck do I accomplish this? I know how to set it up so it will give the id numbers but I have no clue how to I guess convert it? So it shows their names instead? I considered just making another table and then querying from that instead but I’m fairly certain I’ll get marked off for that.

Any help would be greatly appreciated.

Data from person table:

Data from message table:

Expected Result: The table would be too large to show but essentially I need to show every single message Michael Phelps sent and the table needs to show his first and last name, the recipient’s first and last name, the message id, the message itself, and the timestamp for when the message was sent.

My initial attempt was along the lines of;

Unfortunately, it just doesn’t work.

Advertisement

Answer

I hope you haven’t learnt this join syntax in class. Comma-separated joins were used in the 1980s and made redundant in Standard SQL 1992.

Your error is the comma here:

and here:

This is how your query should be written:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement