我有一个数组$DeptID:array(5) { [0]=> string(1) "2" [1]=> string(1) "8" [2]=> string(2) "11" [3]=> string(2) "15" [4]=> string(2) "17" }
然后,我想从MySQL数据库中选择,以获取数组中DeptID所在的数据。我的问题:
$DeptdID = implode(',', $DeptID);
$this->db->select(*)
->from('tabel_data')
->where('DeptID IN ('.$DeptID.')')
->group_by('DeptID', 'ASC')
->get(''):
但是发生了一个错误。
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near (Array) GROUP BY `DeptID` at line 6.
也许有人能给我个解决办法
最佳答案
因为您使用的是CodeIgniter,所以可以尝试使用where_in
函数而不是implode
和where
。where_in
方法将为您实现这一点。
$this->db->select(*)
->from('tabel_data')
->where_in('DeptID', $DeptID)
->group_by('DeptID', 'ASC')
->get(''):