PHPで2つのテーブルを結合する方法-2番目のテーブルのIDと一致し、テーブル1に特定のレコードを表示するMySQL?

CodeSurfer

2つのテーブル(アカウントとレストラン)があります。2つのテーブルのすべてのレコードを表示し、一部のレコードを除外するにはどうすればよいですか?

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の役割を持つこれら2つのテーブルのレコードを表示したいと思います。ただし、ロールがresto_ownerの場合はresto_nameを表示し、adminの場合は空白にし、customerの場合は表示しません。

INNER JOINを使用しようとしましたが、表示されるのは次のとおりです。2Steve 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

詳細はhttps://www.w3schools.com/sql/sql_join_left.aspをご覧ください

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ