我有两张桌子:PROFESSOR和STUDENT。两者都有LEVEL列,其中包含","划界价值。如果我有一个学生,我怎么能写一个查询来返回所有的PROFESSORs匹配至少1个STUDENT.LEVEL?
例如:
STUDENT[1].LEVEL = '1,2'
PROFESSOR[1].LEVEL = '2, 3'
PROFESSOR[2].LEVEL = '2, 4'
我想要返回PROFESSOR 1和2。
答案 0 :(得分:1)
正如D Stanley所说,您需要规范化您的数据。
但无论如何,你的问题有一个解决方案。使用REPLACE
进行数据清算,使用REGEXP
进行加入。
-- data for example
WITH
student AS (
SELECT 'Jon' AS "name", '1,2' AS "level" FROM dual UNION ALL
SELECT 'Bob' AS "name", '1,5' AS "level" FROM dual
),
professor AS (
SELECT 'M.Fowler' AS "name", '2, 3' AS "level" FROM dual UNION ALL
SELECT 'E.Gamma' AS "name", '2, 4' AS "level" FROM dual
)
-- query
SELECT s.*, p.*
FROM student s,
professor p
WHERE REGEXP_LIKE(p."level", REPLACE(REPLACE(s."level",' ', ''), ',', '|'))