您可以使用LOCATE
或FIND_IN_SET
功能。LOCATE 将找到部分匹配项;例如,它将找到 in 的匹配me
项me1,me2
。如果这不是您想要的,也许您正在寻找FIND_IN_SET
.
SELECT lg
FROM a, b
WHERE a.id = 22 AND
LOCATE(b.lg, a.gr) > 0;
复制您发布的数据,这是 SELECT 的输出:
mysql> create temporary table a (gr varchar(100));
Query OK, 0 rows affected (0.36 sec)
mysql> create temporary table b (lg varchar(100));
Query OK, 0 rows affected (0.08 sec)
mysql> insert into a values ('m301,you,weall'),('me,you,me2'),('me');
Query OK, 3 rows affected (0.11 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> insert into b values ('m301'),('you'),('weall'),('me2'),('me'),('me1');
Query OK, 6 rows affected (0.06 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> SELECT lg, gr FROM a, b WHERE locate(lg,gr) > 0;
+-------+----------------+
| lg | gr |
+-------+----------------+
| m301 | m301,you,weall |
| you | m301,you,weall |
| you | me,you,me2 |
| weall | m301,you,weall |
| me2 | me,you,me2 |
| me | me,you,me2 |
| me | me |
+-------+----------------+
7 rows in set (0.02 sec)