[英]How to use array value in where clause in SQL
我有一个数组$rest_id
有3个ID,但是当我做foreach并把它放在我的sql查询中时,我调试它时只出现一个值。 这是代码。
$ids = array();
foreach ($rest_id as $value) {
$ids[] = $value->id;
$nearest_rest = DB::select("SELECT *, (3956 * 2 * ASIN(SQRT( POWER(SIN(( 28.5812674 - lat) * pi()/180 / 2), 2) +COS( 28.5812674 * pi()/180) * COS(lat * pi()/180) * POWER(SIN(( 77.3181059 - lng) * pi()/180 / 2), 2) ))) as distance from restaurant_details where id In ('" . implode("','",$ids) . "') having distance order by distance asc limit 1");
dd($nearest_rest);
}
我想你需要这样的东西,如果你想从3 ids中找到最近的餐馆:
$ids = array();
foreach ($rest_id as $value) {
$ids[] = $value->id;
}
$nearest_rest = DB::select("SELECT *, (3956 * 2 * ASIN(SQRT( POWER(SIN(( 28.5812674 - lat) * pi()/180 / 2), 2) +COS( 28.5812674 * pi()/180) * COS(lat * pi()/180) * POWER(SIN(( 77.3181059 - lng) * pi()/180 / 2), 2) ))) as distance from restaurant_details where id In ('" . implode("','",$ids) . "') having distance order by distance asc limit 1");
dd($nearest_rest);
您需要使用$ids = implode(',',$ID)
函数将数组转换为字符串。
然后,您可以在查询中使用where id in ($ids)
中的where id in ($ids)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.