1

I'm new to SQL and Redash, and I'm currently working on a query that retrieves phone numbers given the ID as shown below

SELECT "ID", "Phone_Number"
FROM Table
WHERE
"id" = '7559' OR
"id" = '1754' OR
"id" = '8679' OR
"id" = '6606' OR
....
....
"id" = '4166' OR
"id" = '7066' OR
"id" = '0027';

I have to manually type the "id" = '4166' OR clause every time I need to retrieve a phone number.

Whats an efficient way to do this?

Please Advise.

The Singularity
  • 2,428
  • 3
  • 19
  • 48
  • `WHERE id IN ('7559', '1754', ... , '0027')`? But why are your ids strings? They look like they should be integers? And do you need to retrieve the same ids every time? (I'm not sure I understand the "manually type" part of your question.) – MatBailie May 18 '21 at 13:35
  • 1
    Where do all the ids in your query come from? if you are just trying to get a more elegant solution then `SELECT "ID", "Phone_Number" FROM Table WHERE "id" IN ('7559', '1754', ...)`. But you will still need to provide the list of IDs you want to filter – Matteo Zanoni May 18 '21 at 13:35
  • @MatteoZanoni the IDs come from a CSV and I use them to query the database. Your solution looks like it'll save me time! Cheers! – The Singularity May 18 '21 at 13:42
  • @MatBailie I'm not sure why they are strings either. But removing the quotes around the ID yields `Error running query: operator does not exist: character varying = bigint LINE 11: "id" IN (7559, 7754) ^ HINT: No operator matches the given name and argument types. You might need to add explicit type casts.` – The Singularity May 18 '21 at 13:44
  • @MatBailie By `Manually Type` I mean it takes a lotta work typing a huge clause for a single ID – The Singularity May 18 '21 at 13:45
  • 1
    If its from a csv file, almost any language can write the query for you, be it using `OR` or `IN()`. By writing a script to do this for you, you avoid typos and other human mistakes. – MatBailie May 18 '21 at 13:55

1 Answers1

0

I finally used @Matteo Zanoni's method:

SELECT "ID", "Phone_Number"
FROM Table
WHERE "id" IN (
'7559',
'1754',
...
)
The Singularity
  • 2,428
  • 3
  • 19
  • 48