简体   繁体   中英

MYSQL SELECT QUERY where column is the same

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.

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'";
 SELECT driver_id FROM T1 WHERE team_id = driver_id AND team_id = @user_driver_one

If you're gonna use PHP it would look like this:

mysql_query("SELECT driver_id FROM tablename WHERE team_id = '$user_driver_one'");

Of course, some sanitation before doing a query would be smart, especially if you're assigning the $user_driver_one variable from a user input.

select driver_id from yourtablename where team_id in (select team_id from yourtablename where driver_id=4)and driver_id!=4

您可以使用$ user_driver_one代替4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM