使用单个变量查找表行,其中表列是数组


use single variable to find table row where the tables column is an array

$sql = mysql_query("SELECT * FROM leg 
WHERE trailers = '$VehicleID')

$VehicleID是单个变量。

trailers 是由 1 个或多个变量组成的数组的列

目前,它只返回在拖车列中只有 1 $VehicleID变量的表行。

如何使拖车返回数组中包含$VehicleID变量的所有行?

使用IN子句和implode函数。

$VehicleID = array(1, 4, 5, 6);
$sql = mysql_query("SELECT * FROM leg 
WHERE trailers IN (" . implode(',', $VehicleID) . ")");

您需要检查更多状态:
1(有一个数字:trailers = $VehicleID
2(数字在字段的开头:trailers LIKE '$VehicleID,%'
3(数字在字段末尾:trailers LIKE '%,$VehicleID'
4(数字在字段中的中间:trailers LIKE '%,$VehicleID,%'

现在你需要使用OR子句使用所有选项(请不要忘记SQL注入(:

$sql = mysql_query("SELECT * FROM leg 
WHERE trailers =     $VehicleID    OR
      trailers LIKE '$VehicleID,%' OR
      trailers LIKE '%,$VehicleID' OR
      trailers LIKE '%,$VehicleID,%'

正确的数据库架构:
创建新表,例如。 trailers 带 2 列: trailer_idvehicle_id

在这种情况下,当您有 in $VehicleID for 值时,此新表中将有 4 行。