データベースに関連する 3 つのテーブルがあります。Grammars
、Users
、およびOrganizations
。
テーブルは、タイプであり、からの外部キーであるフィールドを介しGrammars
てテーブルに関連付けられています。次に、テーブルに関連するテーブルがあります。タイプのテーブル フィールドは、テーブルへの外部キーです。たとえば、 で文法を選択し、対応する情報を選択してから、ユーザーが属する対応する情報を選択するにはどうすればよいですか??users
creator
int
users id
organizations
users
Users
organization
id
organizations id
id
creator
organization
describe grammars;
+------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| short_name | varchar(255) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| domain | int(10) unsigned | YES | MUL | NULL | |
| url | varchar(255) | YES | | NULL | |
| seo_friendly_url | varchar(255) | NO | | NULL | |
| encoding | int(11) unsigned | YES | MUL | NULL | |
| distribution | int(11) unsigned | YES | MUL | NULL | |
| project | int(11) unsigned | YES | MUL | NULL | |
| creator | int(11) unsigned | YES | MUL | NULL | |
| language | int(11) unsigned | YES | MUL | NULL | |
| link | varchar(255) | NO | | NULL | |
| creation_date | datetime | NO | | NULL | |
| last_update | datetime | NO | | NULL | |
| schema | varchar(255) | YES | | NULL | |
| type | int(11) unsigned | YES | MUL | NULL | |
| organization | int(11) unsigned | YES | MUL | NULL | |
+------------------+------------------+------+-----+---------+----------------+
describe Users;
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| first_name | varchar(20) | NO | | NULL | |
| last_name | varchar(40) | NO | | NULL | |
| username | varchar(30) | NO | MUL | NULL | |
| password | varbinary(32) | NO | | NULL | |
| email | varchar(80) | NO | MUL | NULL | |
| organization | int(11) unsigned | YES | MUL | NULL | |
| group | int(11) unsigned | NO | MUL | 1 | |
+--------------+------------------+------+-----+---------+----------------+
describe Organizations;
+--------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| department | varchar(255) | NO | | NULL | |
| communication_info | varchar(255) | NO | | NULL | |
+--------------------+------------------+------+-----+---------+----------------+
やってみると
SELECT Grammars.name AS name, Grammars.description AS description,
Domains.name AS domain,Types.name AS type, Languages.name AS language,
Encodings.name AS encoding,
Organizations.name AS organization_name, Organizations.department AS organization_department, Organizations.communication_info AS organization_comm_info,
Users.last_name AS creator_last_name, Users.first_name AS creator_first_name, Users.email AS creator_email,
Projects.name AS project_name, Projects.short_name AS project_short_name, Projects.url AS project_url,
Projects.funding AS project_funding, Projects.funder AS project_funder,
Projects.start_date AS project_start_date, Projects.end_date AS project_end_date
FROM Grammars
LEFT JOIN Domains ON Grammars.domain=Domains.id
LEFT JOIN Types ON Grammars.type=Types.id
LEFT JOIN Languages ON Grammars.language=Languages.id
LEFT JOIN Projects ON Grammars.project = Projects.id
LEFT JOIN Encodings ON Grammars.encoding = Encodings.id
LEFT JOIN Users ON Grammars.creator = Users.id
LEFT JOIN Organizations ON Users.organization = Organizations.id
WHERE Grammars.id = 3 ;
値がある間、空のセットを取得します。助言がありますか??