如何加入 PHP 中的两个 table - MySQL 匹配第二个 table 中的 ID,并且仍然显示 table 1 中的特定记录?
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,如果是管理员,则显示空白,如果是客户
,则不显示
我尝试使用 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
中的更多内容
我有两个表(帐户和餐厅)如何显示两个表中的所有记录但排除一些记录?
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,如果是管理员,则显示空白,如果是客户
,则不显示我尝试使用 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
中的更多内容