[英]How to JOIN MySQL tables with specific ID of each table with PHP
[英]How to join two tables in PHP - MySQL that match an ID in the second table and still display specific record in table 1?
我有兩個表(帳戶和餐廳),如何顯示兩個表中的所有記錄,但排除一些記錄?
table: account
+-------+----------+------------+
| uid | name | role |
+-------+----------+------------+
| 1 | John | Admin |
| 2 | Steve | Resto_Owner|
| 3 | Bill | Customer |
+-------+----------+------------+
table: restaurant
+--------+----------+------------+
|resto_id| uid | resto_name |
+--------+----------+------------+
| 1 | 2 |Steve Resto |
+--------+----------+------------+
**This is my Desired Output:**
+-------+----------+------------+--------------+
| uid | name | role | resto_name |
+-------+----------+------------+--------------+
| 1 | John | Admin | |
| 2 | Steve | Resto_Owner| Steve Resto |
+-------+----------+------------+--------------+
我想以角色admin和resto_owner顯示這兩個表中的記錄。 但如果角色為resto_owner,則還顯示resto_name;如果為admin,則顯示空白;如果客戶,則不顯示
我嘗試使用INNER JOIN,但它僅顯示: 2 Steve Resto_Owner Steve Resto ,並且不顯示管理記錄:
先感謝您 :)
與條件一起使用左聯接
SELECT account_table.uid, account_table.name, account_table.role, restaurant_table.resto_name
FROM account account_table LEFT JOIN restaurant restaurant_table
ON restaurant_table.uid = account_table.uid
WHERE account_table.role <> 'Customer' ORDER BY account_table.uid ASC
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.