0

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
2
  • 3
    you should normalize your available_vehicles column Commented Feb 17, 2018 at 10:26
  • And think about this: Can teams share a vehicle? Commented Feb 17, 2018 at 10:27

1 Answer 1

2

You can use find_in_set-function, but that would be quite inefficient for larger datasets.

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)
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.