简体   繁体   English

如果在 db2 sql 查询中的上一个表中没有找到记录,则对下一个表执行列搜索

[英]Perform column search on next table if record not found in previous table in db2 sql query

I have three tables with same columns我有三个具有相同列的表

table 1
    col1 col2 col3 col4

table 2
    col1 col2 col3 col4

table 3
    col1 col2 col3 col4

I have to perform a search if record is not found on table1 then only go to search in table2 and if not found in table2 then go to table3 .如果在table1上找不到记录,我必须执行搜索,然后只有 go 在table2中搜索,如果在table2中找不到,则go到 table3 。 But if record found in any of these table then perform some calculation on col4 and return col4 without execution further.但是,如果在这些表中的任何一个中找到记录,则对 col4 执行一些计算并返回 col4 而不进一步执行。 I am using DB2 but not able to find the exact solution.我正在使用 DB2 但无法找到确切的解决方案。 How can i achieve this?.我怎样才能做到这一点?

You can try this:你可以试试这个:

select col1 col2 col3 col4
from (
   select col1 col2 col3 col4, 1 as lvl
   from table_1
   where some_condition  

   union all

   select col1 col2 col3 col4, 2 as lvl
   from table_2
   where some_condition

   union all

   select col1 col2 col3 col4, 3 as lvl
   from table_3
   where some_condition) as t
order by lvl
limit 1 

If you want to keep this as one query, you can use UNION ALL to get the correct table:如果要将其保留为一个查询,可以使用UNION ALL来获取正确的表:

SELECT col4, 1 as SortCol
    FROM Table1
    WHERE col1 = 'whatever'
UNION ALL
SELECT col4, 2 as SortCol
    FROM Table2
    WHERE col1 = 'whatever'
UNION ALL
SELECT col4, 3 as SortCol
    FROM Table3
    WHERE col1 = 'whatever'

ORDER BY SortCol
FETCH 1 ROW ONLY;

EDIT Another method is possible.编辑另一种方法是可能的。 I must say, I'm unsure coming from SQL Server the exact syntax, but it would be something like this:我必须说,我不确定来自 SQL 服务器的确切语法,但它会是这样的:

SELECT COALESCE(t1.col4, t2.col4, t3.col4)

FROM (VALUES (@col1, @col2) ) v(col1, col2)
LEFT JOIN Table1 t1 ON t1.col1 = v.col1 AND t1.col2 = v.col2
LEFT JOIN Table2 t2 ON t2.col1 = v.col1 AND t2.col2 = v.col2
    AND t1.col4 IS NULL
LEFT JOIN Table3 t3 ON t3.col1 = v.col1 AND t3.col2 = v.col2
    AND t1.col4 IS NULL AND t2.col4 IS NULL;

The idea being to use the VALUES clause (or a SELECT with no FROM ) as a driving row.想法是使用VALUES子句(或没有FROMSELECT )作为驱动行。

Similar to what the other posters suggest, you could use code like this if you explicitly want to follow your "if then" logic与其他海报的建议类似,如果您明确想要遵循“如果那么”逻辑,则可以使用这样的代码

CREATE TABLE TABLE_1(C1 INT, C2 INT, C3 INT, C4 INT)

CREATE TABLE TABLE_2(C1 INT, C2 INT, C3 INT, C4 INT)

CREATE TABLE TABLE_3(C1 INT, C2 INT, C3 INT, C4 INT)

WITH
  C4(C1, C2, C3, C4) AS (VALUES (1,2,3,4))
, T1 AS ( SELECT '1' AS LVL, * FROM TABLE_1 JOIN C4 USING (C1, C2, C3, C4) )
, T2 AS ( SELECT '2' AS LVL, * FROM TABLE_2 JOIN C4 USING (C1, C2, C3, C4) WHERE NOT EXISTS (SELECT 1 FROM T1))
, T3 AS ( SELECT '3' AS LVL, * FROM TABLE_3 JOIN C4 USING (C1, C2, C3, C4) WHERE NOT EXISTS (SELECT 1 FROM T2))
, T4 AS ( SELECT '4' AS LVL, * FROM C4                                     WHERE NOT EXISTS (SELECT 1 FROM T3))
SELECT * FROM T1 UNION ALL
SELECT * FROM T2 UNION ALL
SELECT * FROM T3 UNION ALL
SELECT * FROM T4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM