本文介紹了如何在 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
這篇關于如何在 MySQL 中使用外鍵進行查詢?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!