SQL - Select Query for complex dynamic rows

后端 未结 3 1849
你的背包
你的背包 2021-01-24 22:12

I need to retrieve ListingId from the below table based on the search condition. Kindly help the best way to retrive the query for the conditions below

Note : ListingId

3条回答
  •  伪装坚强ぢ
    2021-01-24 22:49

    SELECT
          t1.ListingID
    FROM 
          TableX AS t1 
    
      JOIN                                --- 2nd JOIN
          TableX AS t2
        ON 
          t2.ListingID = t1.ListingID 
    
      JOIN                                --- 3rd JOIN
          TableX AS t3
        ON 
          t3.ListingID = t1.ListingID 
    
    WHERE 
          (t1.ExtraFieldID, t1.Value) = (@ExtraFieldID_search1, @Value_search1)
    
                            --- 2nd condition
      AND 
          (t2.ExtraFieldID, t2.Value) = (@ExtraFieldID_search2, @Value_search2)
    
                            --- 3rd condition
      AND 
          (t3.ExtraFieldID, t3.Value) = (@ExtraFieldID_search3, @Value_search3)
    

    If you need 3 conditions, you'll need to join the table to itself one more time (so 3 times in total)

提交回复
热议问题