How can I query using a foreign key in MySQL?

后端 未结 5 1347
暖寄归人
暖寄归人 2020-12-31 03:54

Right now I have a small database with two tables that look something like this:

    users table
    ====================
    id  name   status_id
    1   Bo         


        
相关标签:
5条回答
  • 2020-12-31 04:17

    You aren't JOINing here:

    SELECT *
    FROM Users U, Statuses S
    WHERE S.id=U.status_ID
    AND status_id = 2;
    
    0 讨论(0)
  • 2020-12-31 04:18

    The easiest way would be through joins:

    select *
    from User u join Status s on u.status_id = s.id;
    

    (if you dont want the status-id at all, you can specify the columns that you do want in the select-clause.)

    0 讨论(0)
  • 2020-12-31 04:22

    Your users table does not have the value of approved in it. It is in your statuses table. When you request status_id you are going to get that value back from that query. You have to do a JOIN ON status_id to make this work out I think. Or do a second query.

    0 讨论(0)
  • 2020-12-31 04:27
    SELECT u.*, s.*
    FROM users u
        inner join statuses s on u.status_id = s.id
    WHERE u.status_id = 2
    
    0 讨论(0)
  • 2020-12-31 04:29

    What you need is this

    SELECT *
    FROM `users`
    JOIN statuses ON statuses.id = users.status_id
    WHERE `status_id` = 2";
    

    and then you can refer to

    $row['value'];
    
    0 讨论(0)
提交回复
热议问题