Home > Mobile >  How to show username if i have 2 tables, user and review
How to show username if i have 2 tables, user and review

Time:01-03

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

CodePudding user response:

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

CodePudding user response:

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

  • Related