I have written the query given below for fetching data with two parameters. Can you suggest the correct SQL syntax for this purpose.
SQL query:
FMResultSet *resultOfDesiredCuisine = [db executeQueryWithFormat:#"select * from dishes where cuisineId IN (select cuisineId from cuisines where Lower(cuisineName) like %#) and
categoryID IN \"(select categoryID from categories where Lower(categoryName) like %#)",cuisineName,categoryName];
SQLite does not work white the %# syntax but uses ?
FMResultSet *resultOfDesiredCuisine = [db executeQueryWithFormat:#"SELECT * FROM dishes WHERE cuisineId IN (SELECT cuisineId FROM cuisines WHERE LOWER(cuisineName) like ?) AND
categoryID IN (SElECT categoryID FROM categories WHERE LOWER(categoryName) LIKE ?)", cuisineName, categoryName];
Related
Is it possible to do orderby expression using linq query expression based on dynamic string parameter? because the query i have is producing weird SQL query
my linq:
var product = from prod in _context.Products
join cat in _context.Categories on prod.CategoryId equals cat.CategoryId
join sup in _context.Suppliers on prod.SupplierId equals sup.SupplierId
orderby sortParam
select new ProductViewModel
{
ProductName = prod.ProductName,
ProductId = prod.ProductId,
QuantityPerUnit = prod.QuantityPerUnit,
ReorderLevel = prod.ReorderLevel,
UnitsOnOrder = prod.UnitsOnOrder,
UnitPrice = prod.UnitPrice,
UnitsInStock = prod.UnitsInStock,
Discontinued = prod.Discontinued,
Category = cat.CategoryName,
Supplier = sup.CompanyName,
CategoryId = cat.CategoryId,
SupplierId = sup.SupplierId
};
where var sortParam = "prod.ProductName"
The code above produces weird sql where order by sortParam is being converted to (SELECT 1). Full query catched by sql profiler below:
exec sp_executesql N'SELECT [prod].[ProductName], [prod].[ProductID], [prod].[QuantityPerUnit], [prod].[ReorderLevel], [prod].[UnitsOnOrder], [prod].[UnitPrice], [prod].[UnitsInStock], [prod].[Discontinued], [cat].[CategoryName] AS [Category], [sup].[CompanyName] AS [Supplier], [cat].[CategoryID], [sup].[SupplierID]
FROM [Products] AS [prod]
INNER JOIN [Categories] AS [cat] ON [prod].[CategoryID] = [cat].[CategoryID]
INNER JOIN [Suppliers] AS [sup] ON [prod].[SupplierID] = [sup].[SupplierID]
ORDER BY (SELECT 1)
OFFSET #__p_1 ROWS FETCH NEXT #__p_2 ROWS ONLY',N'#__p_1 int,#__p_2 int',#__p_1=0,#__p_2=10
I'm seeing a lot of people doing linq order by using dynamic parameter but all of them use lambda not query expression, please enlighten me
As was already mentioned, you are passing a string value instead of an expression that reflects the column name. There are options for what you want however, see for example here.
First of all, I would like to know if it is possible to do?
Below is my query and I am trying to build using criteria.
SELECT CONCAT('record-', rl.record_id) AS tempId,
'sloka' AS type,
rl.record_id AS recordId,
rl.title AS title,
rl.locale as locale,
rl.intro AS intro,
rl.title AS localetitle,
NULL AS audioUrl,
lp.name AS byName,
lp.person_id AS byId,
lp.name AS onName,
lp.person_id AS onId
FROM record_locale rl
LEFT JOIN record r ON rl.record_id = r.record_id
LEFT JOIN locale_person lp ON r.written_on = lp.person_id
WHERE rl.title LIKE :title
AND rl.locale = :locale
AND lp.locale = :locale
UNION
SELECT CONCAT('lyric-', s.song_id) AS tempId,
'bhajan' AS type,
s.song_id AS recordId,
s.title,
l.locale as locale,
NULL AS intro,
l.title AS localetitle,
s.audio_url AS audioUrl,
lpb.name AS byName,
lpb.person_id AS byId,
lpo.name AS onName,
lpo.person_id AS onId
FROM song s
LEFT JOIN locale_person lpb
ON (s.written_by = lpb.person_id AND lpb.locale = :locale)
LEFT JOIN locale_person lpo
ON (s.written_on = lpo.person_id AND lpo.locale = lpb.locale)
INNER JOIN lyric l
ON (l.locale = lpb.locale AND l.song_id = s.song_id)
WHERE s.title LIKE :title AND s.approved_by IS NOT NULL
ORDER BY localeTitle ASC
// END
Based on few conditions, I might need to have union of both queries or just individual query without union.
Converting the SQL to JPQL is usually a good first step, as we can't quite tell what these tables map to, or what you are expecting to get back. If it is possible to do in JPQL, it should be possible with a criteria query. Except in this case: JPA/JPQL does not have the union operator so it won't work in straight JPA, but some providers such as EclipseLink have support. See:
UNION to JPA Query
and
http://www.eclipse.org/eclipselink/documentation/2.5/jpa/extensions/j_union.htm
I am using Join operator to check two values in table but it does not work
select = [[NSString alloc] initWithFormat:#"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID= Category.CategoryID where ContentMaster.ContentTitle='%#' || ContentMaster.ContentTagText='%#' ",appDelegate.tagInput,appDelegate.tagInput];
If i do not use operator and use one in where clause then it shows result.
like
select = [[NSString alloc] initWithFormat:#"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID= Category.CategoryID where ContentMaster.ContentTitle='%#'",appDelegate.tagInput];
select = [[NSString alloc] initWithFormat:#"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID= Category.CategoryID where ContentMaster.ContentTagText='%#'",appDelegate.tagInput];
Any idea how to fix this issue so that or operator works
In SQL (and SQLite in particular), operator || typically means string concatenation.
If you want logical or, use operator OR.
In sqlite just use or for logical or operation:
SELECT *
FROM ContentMaster
LEFT JOIN Category ON ContentMaster.CategoryID = Category.CategoryID
WHERE ContentMaster.ContentTitle = '%#' OR ContentMaster.ContentTagText= '%#'
I have IPhone application in which I am inserting data. It works fine when I fetch all the data without any condition then it works fine if given any condition like
following query then it does not show anything.
NSString *select =
[NSString stringWithFormat:
#"SELECT * from ContentMaster As ml
LEFT JOIN ContentTagging As cat
ON cat.ContentID = ml.ContentID
where cat.ContenTagText= \'%#\'" ,appDelegate.tagInput];
const char *sql = [select UTF8String];
then it does not show any thing using this if i use the select * form ContentMaster then it shows all records added.
Try this.
SELECT * from ContentMaster
LEFT JOIN ContentTagging
ON ContentMaster.ContentID = ContentTagging.ContentID
WHERE ContentTagging.ContenTagText= \'%#\'"
I am using FMDB, which is a wrapper for SQLite. http://github.com/ccgus/fmdb
Here is my query line:
FMResultSet *athlete = [db executeQuery:#"SELECT * FROM athletes WHERE athlete_name LIKE ?", search_text];
Using this I can get a result back if I type in the exact name of the athlete. But, I'm trying to use LIKE so I can search on the name. But, when I add %?% instead of just ? ... nothing returns. And there are no errors.
Has anyone ran into this before and know what I'm doing wrong?
Thanks everyone!
The wildcard characters (%) have to be part of the substituted variable, not the query string:
FMResultSet *rs = [db executeQuery:#"SELECT * FROM athletes WHERE athlete_name LIKE ?",
[NSString stringWithFormat:#"%%%#%%", search_text]];