开发者

MYSQL SELECT QUERY where column is the same

开发者 https://www.devze.com 2023-04-09 12:51 出处:网络
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, sto开发者_Go百科red in a variable $user_dr

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, sto开发者_Go百科red 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

You can use $user_driver_one in place of 4

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号