I have an array where each element corresponds to an alphanumeric string, lets say :
userIds : ['Ab526', 'shvx23', '23636dsd']
I want to convert it into such a format so that I can pass this list of strings to IN clause of mySQL query as something like :
Select * from Users where userIds in '(...)';
I tried using array.join() and many other methods, suggested somewhere or the other but all in vain.
var userIds = ['Ab526', 'shvx23', '23636dsd']
var ids = userIds.join(',');
var query = 'Select * from Users where userIds in (' + ids + ')';
console.log(query);
I have an array where each element corresponds to an alphanumeric string, lets say :
userIds : ['Ab526', 'shvx23', '23636dsd']
I want to convert it into such a format so that I can pass this list of strings to IN clause of mySQL query as something like :
Select * from Users where userIds in '(...)';
I tried using array.join() and many other methods, suggested somewhere or the other but all in vain.
var userIds = ['Ab526', 'shvx23', '23636dsd']
var ids = userIds.join(',');
var query = 'Select * from Users where userIds in (' + ids + ')';
console.log(query);
which results in :
Select * from Users where userIds in ('Ab526, shvx23, 23636dsd');
If anyone could suggest a solution as how can I achieve what I want to, it would be of great help.
Share Improve this question edited Jan 25, 2019 at 8:52 barbsan 3,45811 gold badges23 silver badges29 bronze badges asked Apr 2, 2017 at 8:00 Prerna JainPrerna Jain 1,2502 gold badges16 silver badges35 bronze badges 3-
1
var ids = "'"+userIds.join("','")+"'"
; – mplungjan Commented Apr 2, 2017 at 8:03 - It gives escape character along with single quotes. Tried already – Prerna Jain Commented Apr 2, 2017 at 8:19
- late to party, but your code output is different than provided result - it may be missing something – barbsan Commented Jan 25, 2019 at 8:59
6 Answers
Reset to default 3You could map the quoted values and join later.
var userIds = ['Ab526', 'shvx23', '2363\'6dsd'],
result = userIds.map(function (a) { return "'" + a.replace("'", "''") + "'"; }).join();
console.log(result);
Or a simple one liner
const userIds = [12,13,14,15];
const query = `Select * from Users where userIds in ('${userIds.join("','")}')`;
console.log(query)
You could use reduce on the array:
var userIds = ['Ab526', 'shvx23', '23636dsd'];
var clause = userIds.reduce(
function (cl , a, currIndex, arr)
{
return cl +
(currIndex == 0 ? "" : ",")
+"'"+ a + "'"+
(currIndex == arr.length-1 ? ")" : "") ; } , "(" );
console.log(clause );
You can use the following code :
var userIds = ['Ab526', 'shvx23', '23636dsd'];
var ids = '';
userIds.forEach(function(entry,index) {
ids += (index == 0) ? entry : ',' + entry;
});
console.log(ids);
This worked for me for the same issue:
var myList = [1, 2, 3]
var listAsString = myList.toString()
var queryList = "(" + listAsString + ")"
SQL Query is like so: WHERE number IN queryList
let tickers = ['AAPL', 'MSFT']
const inStmt = "('" + tickers.join("','") + "')"
This will give you the result inStmt == ('AAPL','MSFT') which can be later on used as:
const QUERY = `select x from Y where Y.z in ${inStmt}`
Please note, that if you have numbers, not strings, single quotes must be removed:
let tickers = [1, 2, 3]
const inStmt = "(" + tickers.join(",") + ")"