oracle 用X的列表查询一对X和Y; Y型

2ic8powd  于 2022-11-28  发布在  Oracle
关注(0)|答案(2)|浏览(166)

我们有;
| 职位标识|X轴|Y型|
| - -|- -|- -|
| 一个|-1个|-2个|
| 2个|-1个|-1个|
| 三个|一个|2个|
| 四个|一个|一个|
我要查询列表中的职位:
(2)在一个给定的时间内,将一个变量的值与另一个变量的值进行比较。1)]
我的意料之中:
| 职位标识|X轴|Y型|
| - -|- -|- -|
| 一个|-1个|-2个|
| 四个|一个|一个|

j8yoct9x

j8yoct9x1#

使用多维IN

SELECT *
FROM   table_name
WHERE  (x,y) IN ((-1,-2), (1,1))

或者,如果要将arg作为字符串传递,则使用LIKE

SELECT *
FROM   table_name
WHERE  TRANSLATE('[(-1;-2),(1;1)]', '[]', ',,') LIKE '%,(' || x || ';' || y || '),%'

或者,如果圆括号可以用作术语分隔符,则可以更简单:

SELECT *
FROM   table_name
WHERE  '[(-1;-2),(1;1)]' LIKE '%(' || x || ';' || y || ')%'

其中,对于示例数据:

CREATE TABLE table_name (PositionId, X, Y) AS
SELECT 1, -1, -2 FROM DUAL UNION ALL
SELECT 2, -1, -1 FROM DUAL UNION ALL
SELECT 3,  1,  2 FROM DUAL UNION ALL
SELECT 4,  1,  1 FROM DUAL;

全部输出:
| 职位ID| X轴|Y型|
| - -|- -|- -|
| 一个|-1个|-2个|
| 四个|一个|一个|
fiddle

djmepvbi

djmepvbi2#

如果arg是一个字符串,你可以将它转换成一个数字列表(x,y):

WITH data(positionid, x, y) AS (
    SELECT 1, -1, -2 FROM DUAL UNION ALL
    SELECT 2, -1, -1 FROM DUAL UNION ALL
    SELECT 3, 1, 2 FROM DUAL UNION ALL
    SELECT 4, 1, 1 FROM DUAL -- UNION ALL  
),
expr(s) AS (
    SELECT regexp_replace( '[(-1;-2), (1;1)]', '\[(.*)\]', '\1') FROM DUAL 
),
pairs(n, s) AS (
    SELECT LEVEL, TRIM(regexp_replace( TRIM(regexp_substr(s,'[^,]+',1,LEVEL)) , '\((.*)\)', '\1')) as s
    FROM expr 
    CONNECT BY regexp_substr(s,'[^,]+',1,LEVEL) IS NOT NULL
)
, lopairs(x,y) AS (
    SELECT x, y FROM (
        SELECT n, LEVEL AS c, TO_NUMBER(regexp_substr(s,'[^;]+',1,LEVEL)) AS v
            FROM pairs
            CONNECT BY regexp_substr(s,'[^;]+',1,LEVEL) IS NOT NULL AND PRIOR n = n AND PRIOR SYS_GUID() IS NOT NULL
    )
    PIVOT (
        MAX(v) FOR c IN (1 AS x, 2 AS y)
    )    
)
SELECT positionid, x, y FROM data
WHERE (x,y) IN (
    SELECT x,y FROM lopairs
)
;

相关问题