0

I've got a lot of new data coming into my program that I need to quickly associate with existing data in a table so I can then filter out the approved: false ones.

Here's as distilled as I can think to make the situation:

Given the following:

const list = [{id: 1}, {id: 2}]
// assume ~10K items, not necessarily 1-1 with the MyTable's rows.
INSERT INTO MyTable (id, approved) VALUES (1, TRUE), (2, FALSE), (3, TRUE);
-- assume ~10K rows

How to get to this?

const result = [{id: 1, approved: true}, {id: 2, approved: false}] // only ids from list

I'm asking what SQL query will get from the givens to the result.

mLuby
  • 673
  • 7
  • 15
  • 1
    Your `result` looks like just a `SELECT * FROM MyTable`. Did you have any logic behind your query? Also, are you asking about SQL here or Node? – Tim Biegeleisen Oct 04 '18 at 02:57
  • `result` should only contain ids that were in `list`, not all ids in `MyTable`. I'll edit the question to clarify that. – mLuby Oct 05 '18 at 17:24
  • So why doesn't result also contain "3"? Where'd that guy go? – JNevill Oct 05 '18 at 17:26
  • Or is this just something like `SELECT * FROM mytable where mytable.id in (your list)`? Like... you don't ACTUALLY want to insert here...? – JNevill Oct 05 '18 at 17:27
  • I hear that `WHERE MyTable.id IN (1, 2, etc)` is very slow for thousands of ids. – mLuby Oct 05 '18 at 17:32

1 Answers1

0

This question is a specific instance of SQL WHERE ID IN (id1, id2, …, idn), so the answer is simply dump the ids IN the statement:

const idsString = list.map(item => item.id).join() // sanitize!
const result = sql("SELECT id, approved FROM MyTable WHERE id IN (" + idsString + ");")
mLuby
  • 673
  • 7
  • 15