如何从具有多个关联的两个 MySQL 表中检索数据
How to retrieve data from two MySQL tables with multiple associations
我得到了这两个 table,其中一个 table 有多个指向第二个 table 的外键。
Table rankings
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| search_text | varchar(255) | YES | MUL | NULL | |
| first_item_id | bigint(20) | YES | MUL | NULL | |
| second_item_id | bigint(20) | YES | MUL | NULL | |
| third_item_id | bigint(20) | YES | MUL | NULL | |
| forth_item_id | bigint(20) | YES | MUL | NULL | |
+----------------+--------------+------+-----+---------+----------------+
Table item
+---------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| item_code | varchar(255) | YES | MUL | NULL | |
+----------------+--------------+------+-----+---------+---------------------------+
一个 ranking
记录可能与 item
table 有多个关联,使用 first_item_id
、second_item_id
、third_item_id
或 forth_item_id
字段。我想用相应的 item_code
而不是 item.id
检索 ranking
记录。如果我有大量数据,最有效的方法是什么?
PS:item.id
有 10 个关联,如 first_item_id
... tenth_item_id
。我正在使用 Rails ActiveRecord
ORM。任何解决方法也可以。
样本数据ranking
SELECT id,search_text,first_item_id as first,second_item_id as second,third_item_id as third,forth_item_id as forth from rankings limit 10;
+----+-------------+-------+--------+-------+-------+
| id | search_text | first | second | third | forth |
+----+-------------+-------+--------+-------+-------+
| 1 | test 1 | 1 | 2 | 3 | 4 |
| 2 | test 2 | 1 | 2 | 3 | 4 |
| 3 | test 3 | 1 | 2 | 3 | 4 |
| 4 | test 4 | 1 | 2 | 3 | 4 |
+----+-------------+-------+--------+-------+-------+
样本item
数据
SELECT id,item_code from items limit 5;
+--------+------------+
| id | item_code |
+--------+------------+
| 1 | 125659 |
| 2 | 125660 |
| 3 | 125661 |
| 4 | 125662 |
+--------+------------+
预期数据
+----+-------------+-------+--------+-------+-------+
| id | search_text | first | second | third | forth |
+----+-------------+-------+--------+-------+-------+
| 1 | test 1 | 125659| 125660 | 125661| 125662|
| 2 | test 2 | 125659| 125660 | 125661| 125662|
| 3 | test 3 | 125659| 125660 | 125661| 125662|
| 4 | test 4 | 125659| 125660 | 125661| 125662|
+----+-------------+-------+--------+-------+-------+
加入 table 多次(甚至很多次)应该不是问题,因为您是在主键上加入,即您有一个将要使用的索引。
select
r.id,
r.search_text,
i1.item_code as item_code_1,
i2.item_code as item_code_2,
i3.item_code as item_code_3,
i4.item_code as item_code_4
from rankings r
left join item i1 on i1.id = r.first_item_id
left join item i2 on i2.id = r.second_item_id
left join item i3 on i3.id = r.third_item_id
left join item i4 on i4.id = r.forth_item_id
order by r.id;
我在这里使用外连接,因为您的所有项目列都可以为空。
我得到了这两个 table,其中一个 table 有多个指向第二个 table 的外键。
Table rankings
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| search_text | varchar(255) | YES | MUL | NULL | |
| first_item_id | bigint(20) | YES | MUL | NULL | |
| second_item_id | bigint(20) | YES | MUL | NULL | |
| third_item_id | bigint(20) | YES | MUL | NULL | |
| forth_item_id | bigint(20) | YES | MUL | NULL | |
+----------------+--------------+------+-----+---------+----------------+
Table item
+---------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| item_code | varchar(255) | YES | MUL | NULL | |
+----------------+--------------+------+-----+---------+---------------------------+
一个 ranking
记录可能与 item
table 有多个关联,使用 first_item_id
、second_item_id
、third_item_id
或 forth_item_id
字段。我想用相应的 item_code
而不是 item.id
检索 ranking
记录。如果我有大量数据,最有效的方法是什么?
PS:item.id
有 10 个关联,如 first_item_id
... tenth_item_id
。我正在使用 Rails ActiveRecord
ORM。任何解决方法也可以。
样本数据ranking
SELECT id,search_text,first_item_id as first,second_item_id as second,third_item_id as third,forth_item_id as forth from rankings limit 10;
+----+-------------+-------+--------+-------+-------+
| id | search_text | first | second | third | forth |
+----+-------------+-------+--------+-------+-------+
| 1 | test 1 | 1 | 2 | 3 | 4 |
| 2 | test 2 | 1 | 2 | 3 | 4 |
| 3 | test 3 | 1 | 2 | 3 | 4 |
| 4 | test 4 | 1 | 2 | 3 | 4 |
+----+-------------+-------+--------+-------+-------+
样本item
数据
SELECT id,item_code from items limit 5;
+--------+------------+
| id | item_code |
+--------+------------+
| 1 | 125659 |
| 2 | 125660 |
| 3 | 125661 |
| 4 | 125662 |
+--------+------------+
预期数据
+----+-------------+-------+--------+-------+-------+
| id | search_text | first | second | third | forth |
+----+-------------+-------+--------+-------+-------+
| 1 | test 1 | 125659| 125660 | 125661| 125662|
| 2 | test 2 | 125659| 125660 | 125661| 125662|
| 3 | test 3 | 125659| 125660 | 125661| 125662|
| 4 | test 4 | 125659| 125660 | 125661| 125662|
+----+-------------+-------+--------+-------+-------+
加入 table 多次(甚至很多次)应该不是问题,因为您是在主键上加入,即您有一个将要使用的索引。
select
r.id,
r.search_text,
i1.item_code as item_code_1,
i2.item_code as item_code_2,
i3.item_code as item_code_3,
i4.item_code as item_code_4
from rankings r
left join item i1 on i1.id = r.first_item_id
left join item i2 on i2.id = r.second_item_id
left join item i3 on i3.id = r.third_item_id
left join item i4 on i4.id = r.forth_item_id
order by r.id;
我在这里使用外连接,因为您的所有项目列都可以为空。