如何在 MySQL 中使用外键进行查询?
现在我有一个包含两个表的小型数据库,看起来像这样:
Right now I have a small database with two tables that look something like this:
users table
====================
id name status_id
1 Bobby 3
2 James 2
和
statuses table
=============
id value
1 Waiting
2 Approved
3 Other
status_id 被设置为状态表中 id 的外键约束.我的查询看起来像这样:
status_id is setup as a foreign key constraint to id from the statuses table. My query looks something like this:
SELECT *
FROM `users`
WHERE `status_id` = 2";
当我显示 $row['status_id']
它输出 2
但我希望它显示为 Approved
代替,什么是实现这一目标的最佳方法是什么?
When I display $row['status_id']
it outputs 2
but I would like it to display as Approved
instead, what is the best way to accomplish this?
推荐答案
SELECT u.*, s.*
FROM users u
inner join statuses s on u.status_id = s.id
WHERE u.status_id = 2
相关文章