簡體   English   中英

mysql join從逗號分隔的值中獲取列值

[英]mysql join get column value from comma separated value

我有兩個表,如下所示:

車輛 :

vehicle_id   number_plate
1            B 101 RA
2            B 501 JU
3            B 401 JA
4            B 201 RU

團隊:

team_id      team_name    available_vehicles
1            A-001        1,2
2            A-002        NULL
3            A-003        4

我想獲得價值在團隊表中車輛位置的位置,我想要的輸出如下所示:

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

您可以使用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);

您應該考慮使用單獨的表來創建適當的數據庫結構,以用於

create table team_vehicles (
team_id int, 
vehicle_id int,
primary key(team_id, vehicle_id)
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM