简体   繁体   English

Mysql 来自另一个表的逗号分隔列匹配

[英]Mysql commaseparated column matching from another table

I would like to select all matches from a commaseparated column in table2, where column could be like this: 0,1,2 OR 2,4,5 OR 2,5 OR 1,3,5 etc.我想要 select 表 2 中逗号分隔列的所有匹配项,其中列可能是这样的:0,1,2 OR 2,4,5 OR 2,5 OR 1,3,5 等。

I have tried with:我试过:

SELECT * from table where 1,3,5 IN(SELECT commaseparated FROM table2) WHERE ..

But error on statement when using commas.但是使用逗号时语句出错。

I've also tried using REGEXP but in my case i need to search for all matches within 1,3,5我也尝试过使用 REGEXP 但在我的情况下我需要搜索 1,3,5 内的所有匹配项

How can i solve this one?我该如何解决这个问题? :) :)

Can't do that in standard SQL. it's在标准 SQL 中不能这样做。它是

   WHERE singlevalue IN (list, of, values)

if you want to compare lists against lists, you should revamp your tables so they're properly normalized.如果你想将列表与列表进行比较,你应该修改你的表,使它们正确规范化。 Storing formatted data in a field basically negates the purpose of having a relational database - you can't establish relationships with the data if it's not in a format that allows relationships to be formed.在字段中存储格式化数据基本上否定了拥有关系数据库的目的 - 如果数据不是允许形成关系的格式,则无法与数据建立关系。

If those CSV lists were in sub-tables, you could do a very simple JOIN query to meet your specifications.如果那些 CSV 列表在子表中,您可以执行一个非常简单的 JOIN 查询来满足您的规范。

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

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