我如何从加入表中查询?

时间:2016-09-05 21:00:03

标签: mysql database

SQL新手,假设这是在我的两个主要实体之间创建JOIN表的正确方法,我是否必须硬编码连接表的插入数据?如何从我的连接表中查询?我使用SQL Fiddle,所以我不确定链接是否正确地生成到我的外键。

CREATE TABLE Organization(
`Organization_id` int NOT NULL,
PRIMARY KEY(`Organization_id`)
);

CREATE TABLE QuestionBank(
`Question_id` int NOT NULL,
`Question_text` VARCHAR(255) NOT NULL,
 PRIMARY KEY(`Question_id`)
);

CREATE TABLE OrganizationQuestion(
`OrganizationQuestion_id` int NOT NULL,
`Question_id` int NOT NULL,
`Organization_id` int NOT NULL,
PRIMARY KEY(`OrganizationQuestion_id`),
FOREIGN KEY(`Question_id`) REFERENCES QuestionBank(`Question_id`),
FOREIGN KEY(`Organization_id`) REFERENCES Organization(`Organization_id`)
);

INSERT INTO Organization(`Organization_id`) VALUES(1);
INSERT INTO QuestionBank(`Question_id`, `Question_text`) VALUES(1, 'How did he perform?');

INSERT INTO OrganizationQuestion(`OrganizationQuestion_id`, `Question_id`, `Organization_id`)
VALUES(1, 1, 1);

1 个答案:

答案 0 :(得分:1)

这是你的加入:

select oq.OrganizationQuestion_id, 
oq.Question_id, 
oq.Organization_id, 
o.Organization_id, 
qb.Question_id, 
qb.Question_text 
from OrganizationQuestion oq 
join Organization o 
on o.Organization_id = oq.Organization_id 
join QuestionBank qb 
on qb.Question_id = oq.Question_id 
+-------------------------+-------------+-----------------+-----------------+-------------+---------------------+
| OrganizationQuestion_id | Question_id | Organization_id | Organization_id | Question_id | Question_text       |
+-------------------------+-------------+-----------------+-----------------+-------------+---------------------+
|                       1 |           1 |               1 |               1 |           1 | How did he perform? |
+-------------------------+-------------+-----------------+-----------------+-------------+---------------------+

这并不是非常有趣,因为你几乎所有的东西都是1。

使用EXPLAIN输出:

+----+-------------+-------+--------+-----------------------------+---------+---------+---------------------------------+------+-------------+
| id | select_type | table | type   | possible_keys               | key     | key_len | ref                             | rows | Extra       |
+----+-------------+-------+--------+-----------------------------+---------+---------+---------------------------------+------+-------------+
|  1 | SIMPLE      | oq    | ALL    | Question_id,Organization_id | NULL    | NULL    | NULL                            |    1 | NULL        |
|  1 | SIMPLE      | o     | eq_ref | PRIMARY                     | PRIMARY | 4       | so_gibberish.oq.Organization_id |    1 | Using index |
|  1 | SIMPLE      | qb    | eq_ref | PRIMARY                     | PRIMARY | 4       | so_gibberish.oq.Question_id     |    1 | NULL        |
+----+-------------+-------+--------+-----------------------------+---------+---------+---------------------------------+------+-------------+

请记住,在使用小表的查询中不使用KEYS(索引)。使用KEYS比使用表扫描需要更长的时间。

要查看表格上的索引:

mysql> show indexes from OrganizationQuestion;
+----------------------+------------+-----------------+--------------+-------------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table                | Non_unique | Key_name        | Seq_in_index | Column_name             | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------------------+------------+-----------------+--------------+-------------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| organizationquestion |          0 | PRIMARY         |            1 | OrganizationQuestion_id | A         |           1 |     NULL | NULL   |      | BTREE      |         |               |
| organizationquestion |          1 | Question_id     |            1 | Question_id             | A         |           1 |     NULL | NULL   |      | BTREE      |         |               |
| organizationquestion |          1 | Organization_id |            1 | Organization_id         | A         |           1 |     NULL | NULL   |      | BTREE      |         |               |
+----------------------+------------+-----------------+--------------+-------------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

请参阅SHOW INDEXEXPLAIN

的MySQL手册页

编辑2完全不同的问题

在INSERT期间禁止内容(例如,一个表中的2个FK ID与邮件“发件人”和“收件人”相同)

请参阅以下Answer以生成

signal sqlstate '45000';