簡體   English   中英

SQL查詢返回與where條件的一部分匹配的列和值

[英]SQL query to return columns and values which match part of a where condition

我試圖找出是否有一種很好的方法來獲取所有列名以及特定行的值,其中滿足部分條件。 也就是說,我想知道我巨大的嵌套AND和OR中的哪些字段的條件,滿足哪些條件及其值。

問題是我實際上是在數據表上使用Dynamic LINQ API,我將必須獲取它來生成該查詢,或者完全執行其他操作以本質上檢查某些形式的用戶定義驗證規則。 如果有人對如何解決這個問題有更好的想法,我將不勝感激。

警告這很丑陋-但這可能對您有用。


Select a.*, b.*, mycond1, mycond2, mycond3
From a
    Inner Join b On a.pk = b.pk
    … rest of normal query …

        -- set of conditions --
    Left Outer Join (select 1 as matched where mycondition1) mycond1
    Left Outer Join (select 1 as matched where mycondition2) mycond2
    Left Outer Join (select 1 as matched where mycondition3) mycond3

-- Relationship between conditions
Where (mycond1.matched is not null or mycond2.matched is not null) and mycond3 is not null

想法是使用相關子查詢來返回1或null,具體取決於條件表達式的各個部分對於該行是否為true。 然后,將各個條件表達式之間的邏輯關系應用於where子句。

如果您要生成SQL而不是手動維護它,那么這可能是可行的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM