NodeJS: Query Database Command with Loop condition

Jones

New Member
#1
I want to run a PostgreSQL database query command with condition OR to filter object by an ID Array.
Mã:
var id_arr = [1, 2, 3]
Result I want to get:
Mã:
SELECT * FROM auth_user WHERE id = 1 OR id = 2 OR id = 3
I tried but it gets syntax error:
Mã:
var querydb = `SELECT * FROM auth_user WHERE ` + for (var i of id) { return id = i } + 'OR'
How can I write this code?
 

Admin

Administrator
Thành viên BQT
#2
Like this?
Mã:
var ids = [1,2,3]
var querydb = `SELECT * FROM auth_user WHERE id IN (${ids})`;
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Chủ Đề Xem Nhiều

Top