SQL - Select Query for complex dynamic rows

后端 未结 3 1852
你的背包
你的背包 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 23:08

    You can use union and distinct quite easy for this. If you're using the ListingId as an input to another query using an IN-clause you don't have to mind the duplicates otherwise you can add

    SELECT DISTINCT ListingId FROM (
      SELECT
        ListingId
      ... -- the rest from below
    ) AS Data
    

    Here's the query to get the listing (with possible duplicates!):

    SELECT
      ListingID
    FROM
      TABLE_NAME
    WHERE
      ExtrafieldId = 1 and Value = 1
    UNION ALL
    SELECT
      ListingID
    FROM
      TABLE_NAME
    WHERE
      ExtrafieldId = 1 AND Value = 1 AND ExtrafieldId = 2 and Value = 7
    UNION ALL
    SELECT
      ListingID
    FROM
      TABLE_NAME
    WHERE
      ExtrafieldId = 4 AND Value = 1999
    

提交回复
热议问题