代码Igniter - 从where_in中删除单引号 [英] Code Igniter - remove single quotes from where_in
问题描述
我有2个查询:
$genres = $this->db->select('Group_Concat(intGenreId) strDJGenres')
->from('tblDJGenres')
->where('intDJId', $this->session->userdata('non_admin_userid'))
->get()
->row();
$results = $this->db->select('tblTracks.*, tblGenres.strName as strGenreName')
->from('tblTracks')
->join('tblGenres', 'tblTracks.intGenreId = tblGenres.intGenreId', 'left')
->where_in('tblTracks.intGenreId', $genres->strDJGenres)
->get()
->result();
第一个查询返回一个字符串,例如
The first query is returning a string such as
'1,2,3,4,8,6,5,7,45,66'
'1,2,3,4,8,6,5,7,45,66'
我在我的where_in子句中使用第二个查询。问题是,使用这个字符串,它正在写如下SQL:
which I am using in my where_in clause on the second query. The issue is that with this string, it is writing the SQL like:
SELECT `tblTracks`.*, `tblGenres`.`strName` as strGenreName FROM (`tblTracks`) LEFT JOIN `tblGenres` ON `tblTracks`.`intGenreId` = `tblGenres`.`intGenreId` WHERE `tblTracks`.`intGenreId` IN ('1,2,3,4,8,6,5,7,45,66')
被视为单个值。如何获得第二个查询执行我想要的?
With the quote around it, it is treated as a single value. How can I get the second query to perform how I want it? ie
.... where `tblTracks`.`intGenreId` IN (1,2,3,4,8,6,5,7,45,66)
推荐答案
可以作为数组
传递到 where_in
子句。
可以使用 trim()
删除字符串开头和结尾的引号:
The quotes from the start and end of the string can be removed using trim()
:
$dj_genres = trim($genres->strDJGenres, "'");
此字符串可以转换为字符串数组 ,以传递到第二个查询的 where_in
子句。
This string can then be converted into an array of strings, to pass to the where_in
clause of the second query.
$dj_genres_array = explode(",", $dj_genres);
或者,如果您需要整数数组 em>:
Or, if you need an array of integers:
$dj_genres_int_array = array_map('intval', $dj_genres_array);
您可以将结果数组添加到第二个查询:
You can just add the resultant array into the second query:
// ...
->where_in('tblTracks.intGenreId', $dj_genres_array)
// ...
或:
// ...
->where_in('tblTracks.intGenreId', $dj_genres_int_array)
// ...
这篇关于代码Igniter - 从where_in中删除单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!