-1

我的数据库中有三个相关的表。Grammars, Users, 和Organizations. 表通过字段与表Grammars相关,该字段是类型并且是来自 的外键。接下来我们有一个表,它又与表相关。类型的表字段是表的外键。如何选择例如语法,然后选择其相应的信息,然后选择用户所属的相应信息?userscreatorintusers idorganizationsusersUsersorganizationidorganizations ididcreatororganization

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 ; 

有值时我得到空集。有什么建议么??

4

1 回答 1

0

此查询不返回任何数据的唯一原因是因为 Grammars 中没有行可以完成: Grammars.id = id

如果您使用 LEFT JOIN,它不会限制语法返回。“id”的值是多少?查看表格的描述,我发现每个表格中都有一个“id”字段...您应该限定此字段名并确保某些行满足条件。

于 2013-01-07T11:19:29.450 回答