[英]mysql join get column value from comma separated value
I have two tables as below : 我有两个表,如下所示:
vehicles : 车辆 :
vehicle_id number_plate
1 B 101 RA
2 B 501 JU
3 B 401 JA
4 B 201 RU
team : 团队:
team_id team_name available_vehicles
1 A-001 1,2
2 A-002 NULL
3 A-003 4
I want to get value where the position of vehicle in the team table, my desired output would look like this: 我想获得价值在团队表中车辆位置的位置,我想要的输出如下所示:
vehicle_id ║ number_plate ║ team_name
1 ║ B 101 RA ║ A-001
2 ║ B 501 JU ║ A-001
3 ║ B 401 JA ║ NULL
4 ║ B 201 RU ║ A-003
You can use find_in_set-function, but that would be quite inefficient for larger datasets. 您可以使用find_in_set-function,但是对于较大的数据集来说效率很低。
select v.vehicle_id, v.number_plate, t.team_name
from vehicles v
join team t on find_in_set(v.vehicle_id, t.available_vehicles);
You should consider creating proper database structure with a separate table for 您应该考虑使用单独的表来创建适当的数据库结构,以用于
create table team_vehicles (
team_id int,
vehicle_id int,
primary key(team_id, vehicle_id)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.