skip to Main Content

i am creating a restaurant review website. in my review table i have a foreign key called user_id and idk how to use it to display the username which is in the user table

my user table
my review table

so my question is how do i display the username from this? what mysql statement do i have to write. I am lost on what to do

2

Answers


  1. If the relationship between the two tables is mapped out correctly you should be able to run a query to fetch the name of each user. Try to avoid any N+1 query though

    Login or Signup to reply.
  2. Assuming you want to try and get the review text along with the user name from the corresponding user you can use a join to combine the info for example:

    SELECT u.username, r.review_text
    FROM reviews r
    LEFT JOIN users u
    ON (u.user_id = r.user_id)
    

    I assumed the users table is called users and reviews table is called reviews but update those as necessary each is "aliased" as u and r respectively and then tables are joined

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search