简体   繁体   English

查询 where_in 逗号分隔列表

[英]Query where_in comma separated list

I have a 'list_table' table looks like:我有一个“list_table”表,如下所示:

id :  list 
1  :  1,2,44,5    
2  :  4,3,5,2,56,66 

Is it possible to check if '44' is in List column in mysql database?是否可以检查“44”是否在 mysql 数据库的列表列中? I'm using codeigniter and my code looks like:我正在使用 codeigniter,我的代码如下所示:

$this->db->select('*'); 
$this->db->from("list_table");
$this->db->where("find_in_set('44', 'list')");
$query = $this->db->get();
return $query->result();

I also tried with WHERE_IN but didn't get correct result.我也试过 WHERE_IN 但没有得到正确的结果。

This is what query I get when I enable_profile:这是我在 enable_profile 时得到的查询:

SELECT *
FROM `poslovi`
LEFT JOIN `firme` ON `firme`.`f_id` = `poslovi`.`po_firma_id`
LEFT JOIN `kategorije` ON `kategorije`.`k_id` = `poslovi`.`po_category`
WHERE `po_date_istek` > '2022-03-21 10:37:25'
AND   (`po_naziv_oglasa` LIKE '%Radnik u ćevabdžinici%' ESCAPE '!' OR  `f_name` 
LIKE '%Radnik u ćevabdžinici%' ESCAPE '!')
AND find_in_set("61", po_category) <> 0
AND `po_status` = '1'
ORDER BY `po_date_istek` DESC
LIMIT 10

This is what I have in my database:这是我的数据库中的内容: 在此处输入图像描述

Just to mention, if I remove 'find_in_set' I get correct result so the rest of the query is good as I noticed顺便提一下,如果我删除“find_in_set”,我会得到正确的结果,所以查询的 rest 正如我注意到的那样是好的

You need a true or false condition in the WHERE clause, so a comparison您需要 WHERE 子句中的 true 或 false 条件,因此比较

$this->db->select('*'); 
$this->db->from("list_table");
$this->db->where("find_in_set('44', 'list') <> 0");
$query = $this->db->get();
return $query->result();

But it ot recomended to store data this way.但不建议以这种方式存储数据。 Read mor in Is storing a delimited list in a database column really that bad?阅读更多在数据库列中存储分隔列表真的那么糟糕吗?

As nbk said, you need the true/false condition, however, the answer is not working for the OP.正如 nbk 所说,您需要真/假条件,但是,答案不适用于 OP。 You need to remove the single quotes around list in that answer:您需要删除该答案list周围的单引号:

$this->db->where("find_in_set('44', 'list') <> 0");

Rewrite the code as below, minus the quotes around list :重写代码如下,去掉list两边的引号:

$this->db->select('*'); 
$this->db->from("list_table");
$this->db->where("find_in_set('44', list) <> 0");
$query = $this->db->get();
return $query->result();

That should solve the issue for you.那应该可以为您解决问题。

In a screenshot you posted of your data , its possible to see that you include a space after each comma.您发布的数据截图中,可以看到每个逗号后都有一个空格。

The value of po_category is 2, 7, 61 , not 2,7,61 —and find_in_set does not ignore those spaces! po_category的值是2, 7, 61 ,而不是2,7,61 ——而且find_in_set不会忽略这些空格!

You've noticed that find_in_set works when you search for the first entry, this is because that does not have a leading space;您已经注意到find_in_set在您搜索第一个条目时有效,这是因为它没有前导空格; 7 and 61 do. 761可以。

find_in_set(" 61", po_category) would match, in this case, but then it wouldn't match if it is the first entry.在这种情况下, find_in_set(" 61", po_category)会匹配,但如果它第一个条目,则它不会匹配。 While you could do (find_in_set("61", po_category) <> 0 || find_in_set(" 61", po_category)) <> 0 to support both cases, that is unnecessarily slow and taxing.虽然您可以(find_in_set("61", po_category) <> 0 || find_in_set(" 61", po_category)) <> 0来支持这两种情况,但这是不必要的缓慢和费力。 Just save your data without spaces.只需不带空格地保存您的数据。 Or, better yet, not as a comma separated list.或者,更好的是,不是以逗号分隔的列表。

In nbk's answer there's a link that explains why doing this is not optimal.nbk 的回答中有一个链接解释了为什么这样做不是最佳的。 One way to save a list of IDs is making a separate table for them and using JOINs.保存 ID 列表的一种方法是为它们创建一个单独的表并使用 JOIN。 This will be better for performance.这对性能会更好。 Another option that is slightly more complex to implement, if you are using MySQL 8.0.17 or higher, is to save it as a JSON array & index that array.如果您使用 MySQL 8.0.17 或更高版本,另一个实施起来稍微复杂一些的选项是将其另存为 JSON 数组并对该数组进行索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM