我有一个像Array一样动态的数组([122] => 2 [123] => 3 [124] => 6)我希望通过这些数组值从数据库中获取记录

时间:2018-03-15 07:00:47

标签: php mysql sql arrays

我的项目是关于商人和客户,客户发布他们的工作选择位置,商人可以找到他所在地区的工作,但条件是商人可以添加多个区域所以问题是有一个商人,其中增加了三个位置就像

$locs=Array ( [122] => 2 [123] => 3 [124] => 6 )

我想从数据库中获取记录,其中这些位置ID可用2,3,6

什么是查询

SELECT j.*, l.location_name, c.customer_name, c.customer_status, s.service_title 
FROM ".TABLE_JOB."  AS j 
LEFT JOIN ".TABLE_LOCATION." AS l ON (l.location_id=j.location_id) 
LEFT JOIN ".TABLE_CUSTOMER." AS c ON (c.customer_id=j.customer_id) 
LEFT JOIN ".TABLE_SERVICE." AS s ON (s.service_id=j.service_id) 
WHERE j.status='PENDING' 
  AND c.customer_status='ACTIVATED' 
  AND j.location_id='".$locs."' 

2 个答案:

答案 0 :(得分:1)

使用implode()函数将数组转换为字符串,然后在mysql查询中转换为in

$locsIds = implode(',', $locs);
select j.*, l.location_name, c.customer_name, c.customer_status, s.service_title from ".TABLE_JOB." as j left join ".TABLE_LOCATION." as l on (l.location_id=j.location_id) left join ".TABLE_CUSTOMER." as c on (c.customer_id=j.customer_id) left join ".TABLE_SERVICE." as s on(s.service_id=j.service_id) where j.status='PENDING' AND c.customer_status='ACTIVATED' and j.location_id IN ({$locsIds})

答案 1 :(得分:0)

您可以使用此代码。

$location = '';

foreach($locs as $key=>$value){
 $location .= $value.","; 
}

$location = rtrim($location,",");

select j.*, l.location_name, c.customer_name, c.customer_status, s.service_title from ".TABLE_JOB." as j left join ".TABLE_LOCATION." as l on (l.location_id=j.location_id) left join ".TABLE_CUSTOMER." as c on (c.customer_id=j.customer_id) left join ".TABLE_SERVICE." as s on(s.service_id=j.service_id) where j.status='PENDING' AND c.customer_status='ACTIVATED' and j.location_id IN ($location);

通过这种方式,您将获得2,3,6。

中location_id的所有记录

希望它有所帮助。