如何在mysql查询中比较两个数组?

时间:2021-07-06 12:08:16

I have a table named mm_test like follows:-

我有一个名为mm_test的表如下: -

id    educations 
1     3,4,6,8,9

I want to check whether this array contain (4,6). How to check this using mysql query. Mysql 'IN' command is not working for me.

我想检查这个数组是否包含(4,6)。如何使用mysql查询检查这个。 Mysql'IN'命令对我不起作用。

I put the query like this...

我把这样的查询...

select * from mm_test where educations IN (4,6);

But this query returned empty result. Please any one help me.

但是这个查询返回了空结果。请任何人帮助我。

2 个解决方案

#1


7  

You can do this using find_in_set():

您可以使用find_in_set()执行此操作:

where find_in_set(4, educations) > 0 and
      find_in_set(6, educations) > 0

However, this is generally inefficient. The problem is your data structure. You should be using a junction table with one column for each education, instead of storing a list of integers in a highly-inappropriate data structure -- a string.

然而,这通常是低效的。问题是你的数据结构。对于每个教育,您应该使用一个包含一列的联结表,而不是将一个整数列表存储在一个非常不合适的数据结构中 - 一个字符串。

#2


0  

Use this FIND_IN_SET Manual

使用此FIND_IN_SET手册

SELECT * 
FROM mm_test 
WHERE FIND_IN_SET(4, educations) > 0 
AND FIND_IN_SET(6, educations) > 0;

#1


7  

You can do this using find_in_set():

您可以使用find_in_set()执行此操作:

where find_in_set(4, educations) > 0 and
      find_in_set(6, educations) > 0

However, this is generally inefficient. The problem is your data structure. You should be using a junction table with one column for each education, instead of storing a list of integers in a highly-inappropriate data structure -- a string.

然而,这通常是低效的。问题是你的数据结构。对于每个教育,您应该使用一个包含一列的联结表,而不是将一个整数列表存储在一个非常不合适的数据结构中 - 一个字符串。

#2


0  

Use this FIND_IN_SET Manual

使用此FIND_IN_SET手册

SELECT * 
FROM mm_test 
WHERE FIND_IN_SET(4, educations) > 0 
AND FIND_IN_SET(6, educations) > 0;