Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL SELECT QUERY where column is the same

Tags:

php

mysql

Hi I am trying to query a table in my database, basically I need to select the driver_id from the table where the team_id is the same as the users driver id, stored in a variable $user_driver_one. So pretty much selecting the other driver with the same team_id.

column driver_id 1 2 3 4 5 6 7 8 9 10

  column team_id 3 3 2 2 1 1 4 4 5 5

So the user could have $user_driver_one = 4, so I need to select in a query the driver_id = 3, as driver_id 3 has the same team_id.

I am having problems with this, any help is much appreciated.

like image 996
user970117 Avatar asked Mar 12 '26 10:03

user970117


1 Answers

Make sure you escape the parameter

$user_driver_one = (int) $user_driver_one;

MySQL:

"SELECT `driver_id` 
 FROM `table` 
 WHERE `team_id` = (
     SELECT `team_id`
     FROM `table`
     WHERE `driver_id` = '$user_driver_one'
     LIMIT 1
    )
 AND `driver_id` != '$user_driver_one'";
like image 70
Chris G. Avatar answered Mar 13 '26 23:03

Chris G.