search

Home  >  Q&A  >  body text

Display username, user and review when there are 2 tables

I'm creating a restaurant review website. In my comments table I have a foreign key called user_id and idk how can I use it to display the username from the users table

My user table My comment form

So my question is how to display the username? What mysql statements do I have to write. I don't know what to do

P粉195200437P粉195200437479 days ago709

reply all(1)I'll reply

  • P粉190883225

    P粉1908832252023-09-10 11:53:52

    Suppose you want to try to get the comment text and username from the corresponding user, you can use a join to combine the information, for example:

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

    I assume the users table is called users and the reviews table is called reviews, but update those tables as needed, each "aliased" to u and r respectively, and then the table Yes has joined

    reply
    0
  • Cancelreply