我有两个表(t1& t2):
t1 (第二列为array)
name | code
ee | 123, 124, 125
ef | 121, 123
______________________
T2
code_id | code_desc
121 | xxxxx
123 | yyyyyyy
124 | xxxxxxxx
如果我进行此查询,一切都可以:
select *从t2中选择 * code_id in(121,122)
但是,如果我进行此查询,我有空单元/结果
从t2中选择 * code_id in(从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
)
不过,我建议您将表结构标准化。因为即使上述查询正在起作用,它也不可降低。