我有两个表(t1 和 t2):
t1(第二列是数组)
name | code
ee | 123, 124, 125
ef | 121, 123
______________________
t2
code_id | code_desc
121 | xxxxx
123 | yyyyyyy
124 | xxxxxxxx
<小时/>
如果我执行此查询,一切正常:
SELECT * FROM t2 where code_id in (121,122)
但是如果我执行此查询,我会得到 NULL 单元格/结果
SELECT * FROM t2 where code_id in (SELECT code FROM t1 where name = ee)
如何从一个查询中获取两个表中的所有信息?
这是代码,我找不到好的sql在线工具
CREATE TABLE t1 (name VARCHAR(200), codes VARCHAR(200));
CREATE TABLE t2 (codes_id VARCHAR(200), codes_desc VARCHAR(200));
INSERT INTO t1 (name, codes) VALUES ('ee', '123,124,125');
INSERT INTO t1 (name, codes) VALUES ('ef', '121,124');
INSERT INTO t1 (name, codes) VALUES ('eh', '123,124,125');
INSERT INTO t2 (codes_id, codes_desc) VALUES ('121', 'yyyyyyyyy');
INSERT INTO t2 (codes_id, codes_desc) VALUES ('122', 'xxxxxxxxx');
INSERT INTO t2 (codes_id, codes_desc) VALUES ('123', 'zzzzzzzzzzz');
SELECT * FROM t2 where code_id in (121,122)
SELECT * FROM t2 where code_id in (SELECT codes FROM t1 where name = 'ee')
最佳答案
您可以使用find_in_set
函数:
select *
from t2
where exists (
select 1
from t1
where name = 'ee'
and find_in_set(t2.code_id, t1.code) > 0
)
不过,我建议您标准化您的表结构。因为即使上面的查询有效,它也是非 sargable .
关于mysql - 选择的结果是数组,我想将它用于 "where in"另一个选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42517895/