我有两张桌子(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,其中code_id位于(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')
答案 0 :(得分:0)
您可以使用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。