I am trying to pass array of cities to the IN clause but seems inverted commas are removed while preparing query
Here is the code
let city = ["Moscow", "Paris"];
let sql = 'SELECT* FROM cities WHERE city IN ('+city+')';
console.log(sql);
and in the console I am getting this
SELECT* FROM cities WHERE city IN (Moscow, Paris);
and thus getting exception while executing query!
Any help?
Try this, it will work
var sql = "SELECT uid FROM cities where city IN ('" + gender.join("','") + "')";
Result :-
SELECT uid FROM cities where city IN ('Male','Female')
Using a Query Builder is a preferable method but a possible solution if you have a long array:
let stringifiedValues = JSON.stringify(city).replace(/\[|\]/g,'');
let sql = `SELECT * FROM cities WHERE city IN (${stringifiedValues})`;
Use the map function to transform all items in your array. You want to add quotes to the items like this Moscow => 'Moscow'
Use the join to gather the transformed items into a string. The delimiter can be specified as an argument like this array.join(', ')
Use template literals (string interpolation) to inject your string in your query. Like this ${str}
const city = ["Moscow", "Paris"];
const injectedString = city.map(c => `'${c}'`).join(', ');
let sql = `SELECT * FROM cities WHERE city IN (${injectedString})`;
console.log(sql);
Related
**## Node ##
Loopback Node**
Like select Fetch Multiple Category Id From Product Table.
Pass your category id in above Bold Parameter
let tempQuery += " `categoryId` IN " + **categoryId**;
var query = "SELECT * FROM `product` WHERE **tempQuery** ";
Above Code will give response Like...
It fetches all categories and gives in response now in temp query variable matches category id with the response and return products if matches any
If In SQL
consider 1,2,3,4,5 as Id
"select * from info WHERE `id` IN ('1,2,3,4,5')"
I find it really difficult to understand your question. Are you trying to write a LoopBack query to match multiple ids? If that's the case, then you can use the inq operator as described here.
const filter = {where: {id: {inq: [1,2,3,4,5]}}};
// LoopBack 3.x
const found = await Category.find(filter);
// LoopBack 4.x
const found = await categoryRepo.find(filter);
I have a mysql query in java like
public static BusObjectIterator<con.PROJECT_EMP> GetEmpObjectsforOrgandMultipleCategory(String ORG, String CATEGORY)
{
String query=select * from PROJECT_EMP where org = :ORG and category=:CATEGORY;
.....
return ...
}
Here the param ORG will have single value like xyz and CATEGORY String may have multiple values like Cat1,Cat2 etc..,
So dynamically i would like to frame query using REGEX to replace the comma separated string values like
select * from PROJECT_EMP where org = 'xyz' and category in ('Cat1','Cat2');
Thanks.
You can use FIND_IN_SET() function instead of RegEx
Try this:
SELECT * FROM PROJECT_EMP WHERE org = :ORG AND FIND_IN_SET(category,:CATEGORY);
I'm trying to get this LINQ to SQL to work. The problem is parsedSeasons is a string like "1,2,3" and h.season is an int column. How can I get this to work correctly?
var id = (from h in db.t_ref_harvest_type
where parsedSeasons.Contains(h.season)
select new { h.id });
You need to first split your comma delimited string like this:
var Seasons = parsedSeasons.Split(',').Select(int.Parse);
Then use your LINQ query:
var id = (from h in db.t_ref_harvest_type
where Seasons.Contains(h.season)
select new { h.id });
I have procedure with a single string parameter to retrieve records from my table test which has two fields id(int) and Name(varchar).
the query in the procedure is shown below
Select * from test where id in (strParam);
and value in the parameter will be
strParam="1,2";
but the result will be wrong because query will be as shown below
Select * from test where id in ('1,2');
but i need the query to be like shown below
Select * from test where id in (1,2);
please help me with a solution
the programming language is C#
thanks,
suraj
Usually you construct the SQL correctly in your programming language:
Select * from test where id in ('1,2');
should come from your application code, where it's easier to change strParam="1,2"; to strParam="'1','2'":
Split (explode) the string into an array
escape each element in the array (using the correct MySQL-ESCAPE function)
Join (implode) the array back into a string,
If you really can't change the application code, maybe some SQL tricks could work. Try:
SELECT * FROM test where FIND_IN_SET(ID,strParam) > 0
Not sure if this is the most efficient way:
Explode the value strParam to an array and then build up the string you need in the query:
<?php
$arrayParam = explode(',', $strParam);
$strParamQuery = '(';
foreach ($arrayParam as $Param) {
if ($strParamQuery != '(') { $strParamQuery = $strParamQuery.','; //Add a comma to all but the first occurence
$strParamQuery = $strParamQuery.$param;
}
$strParamQuery = $strParamQuery.')';
$query = 'Select * from test where id in '.$strParamQuery.';';
?>
I have a string of IDs separated with comma
$myIDs = 22,23,45,895;
How do I write a query to return records for values that correspond to the IDs in my string?
This does not seem to be right:
SELECT *
FROM t1
WHERE itemID IN ($myIDs)
I guess I'm trying PHP array function here, hah? Is there something like this in mySQL?
Appreciate any suggestions. Thanks.
I think you're missing quotes, ie, the exact query should look like this before evaluation
SELECT *
FROM t1
WHERE itemID IN ('22','23','45','895');
Hence all you've got to do to fix this is:-
$myIDs = array(22,23,45,895);
$myIDs_string = "'".implode("','",$myIDs)."'";
then in whatever PHP/SQL library/framework you select, use PHP to execute the following php query:-
SELECT *
FROM t1
WHERE itemID IN ($myIDs_string);
Hope this helps.
$IDs = array(1,2,3,4,5);
// alternatively, you can write it like this...
// $IDs = "1,2,3,4,5";
if(is_array($IDs))
$IDs = implode(",",$IDs);
$query = "SELECT * FROM t1 WHERE itemID IN ($IDs)";
echo $query;