how can I solve the following problem. At the moment I implement an query, which search in db for string in rows. It works great, I can pageing the result in frontend but I can't sort. If I call the following url and add the sort params as url parameter It no sort my result.
How I can solve the problem
web/api/orders/search/findSearch?page=0&sort=number,asc&searchText=hello&size=25
#Query("SELECT o FROM Order o where"+
"(lower(o.number) like('%' || lower(:searchText) || '%') " +
"or lower(o.name) like('%' || lower(:searchText) || '%') ) " +
"or lower(o.created) like('%' || lower(:searchText) || '%') " +)
Page<Order> findSearch(#Param("searchText") String searchText, Pageable pageable);
Related
I am trying to use postgresql jsonb operators with spring data jpa query as:
#Query(value="SELECT * from Employee e WHERE e.details #> '{\"province\":{\"city\":{\"town\": \":town\"}}, \"hobbies\": [\":hobby\"]}'",nativeQuery = true)
town and hobby are inputs.
There is no error but no result is returned, though records are there which meets the criteria
It seems parameter binding is not working.
What can be the solution?
Here, :town and :hobby is inside ''(single quote) means string literal, so parameter can't be replaced. You can use || to concat as string so that parameters are not inside in '' and can be replaced.
#Query(value="SELECT * from Employee e WHERE e.details #> ''||'{\"province\":{\"city\":{\"town\": \"' || :town || '\"}}, \"hobbies\": [\"' || :hobby || '\"]}'||'' ", nativeQuery = true)
I have to create this N1QL couchbase query in Spring data
select... LIKE "TASK:100:%"
where 100 is a parameter, but I don't know if it is possible
#Query("#{#n1ql.selectEntity} where META().id like \"TASK:$1%:\" ")
List<Task> findTasks(String taskId);
The correct syntax would be the following:
#Query("#{#n1ql.selectEntity} where META().id like ('TASK:' || $1 || '%') ")
List<Task> findTasks(String taskId);
Although I think you should concatenate the id on the backend instead of inside the query.
I am working with Android Studio room database. I have successfully used these queries to get names satisfying the query criteria.
#Query("SELECT * FROM malenamesdb WHERE name LIKE '%' || :search || '%' AND selected ")
fun findByName(search: String?): List<MaleNamesDB>
#Query("SELECT * FROM malenamesdb WHERE name LIKE :search || '%' AND selected ")
fun maleNameBeginsWith(search: String?): List<MaleNamesDB>
But when I tried to create a query that is supposed to find all name that ends with a given string like so.
#Query("SELECT * FROM malenamesdb where name LIKE '%' || :search")
fun maleNameEndsWith(search: String?): List<MaleNamesDB>
The result of maleNameEndsWith() function is an empty list, it dosent find anything. No matter what search string is prowidet.
It does not work, why is this not working and what am I doing wrong?
I have a very basic SELECT statement that is causing me some grief.
I have a primary table of tools that I am selecting data from. In that table, I only want tools where the status is not Decommissioned.
From there, I am checking the tool name against my LIKE clause to see if it's a close match. This is done for my AJAX typeahead.
Lastly, it's joining a table called akas which contains tool names that can also be used in reference to the main tool. These are basically other aliases the tools go by.
The issue I am facing is that it is still including records with the Decommissioned status. I am pretty sure its the WHERE clause.. Any thoughts?
SELECT t.toolName,
t.tool AS toolID,
t.toolType,
t.toolStatus,
e1.PreferredName AS ownerFirst,
e1.LastName AS ownerLast,
e1.NTID AS ownerNTID
FROM dbo.ti_tools AS t
LEFT OUTER JOIN
dbo.ti_toolAKAs AS a
ON t.tool = a.tool
LEFT OUTER JOIN dbo.EmployeeTable AS e1
ON t.toolOwner = e1.QID
WHERE (t.toolName LIKE '%' + #tool + '%') OR (a.aka LIKE '%' + #tool + '%')
AND t.toolStatus <> 'Decommissioned'
ORDER BY t.toolName ASC
FOR XML PATH ('tools'), TYPE, ELEMENTS, ROOT ('root');
Order of operations
AND has greater precedence than OR.
Change your WHERE clause to the following, using parentheses around the two OR conditions to separate them logically from the commission check:
WHERE ((t.toolName LIKE '%' + #tool + '%') OR (a.aka LIKE '%' + #tool + '%'))
AND t.toolStatus <> 'Decommissioned'
Your original WHERE clause was being evaluated identically to this:
WHERE (t.toolName LIKE '%' + #tool + '%') OR
((a.aka LIKE '%' + #tool + '%') AND t.toolStatus <> 'Decommissioned')
Add round brackets:
SELECT t.toolName,
t.tool AS toolID,
t.toolType,
t.toolStatus,
e1.PreferredName AS ownerFirst,
e1.LastName AS ownerLast,
e1.NTID AS ownerNTID
FROM dbo.ti_tools AS t
LEFT OUTER JOIN dbo.ti_toolAKAs AS a
ON t.tool = a.tool
LEFT OUTER JOIN dbo.EmployeeTable AS e1
ON t.toolOwner = e1.QID
WHERE ((t.toolName LIKE '%' + #tool + '%') OR (a.aka LIKE '%' + #tool + '%'))
AND t.toolStatus <> 'Decommissioned'
ORDER BY t.toolName ASC
FOR XML PATH ('tools'), TYPE, ELEMENTS, ROOT ('root');
The case is very simple:
cond1 OR cond2 AND cond3
<=>
cond1 OR (cond2 AND cond3)
And you get records that satisfy only cond1. What you really want is:
(cond1 OR cond2) AND cond3
I have a screen in my project " Enquiry" where I user can perform 2 kinds of search.I cannot figure out how to do a "like" or "And or is null" in Linq to entity.
Simple Search ( Search in all fields using "Like" operator)
Advanced Advanced Search (Use "And" Operator)
So lets take these 3 tables and make up a noddy example.
Customer Table (CustomerID,Name,Surname)
Address(AddressID,Street,City)
CustomerOrder (OrderID,OrderName)
Basic search :
this is how I would do it in Sql
SELECT TOP (100) C.Name,C.Surname,CA.Street,CA.City,CO.OrderName
FROM Customer C
LEFT JOIN CustomerAddress CA ON C.CustomerID=CA.CustomerID
LEFT JOIN CustomerOrders CO ON C.CustomerID=CA.CustomerID
WHERE (C.CustomerID LIKE '%' + #SearchText + '%'
OR C.Surname LIKE '%' + #SearchText + '%'
OR C.Name LIKE '%' + #SearchText + '%'
OR CA.Street LIKE '%' + #SearchText + '%'
OR CA.City LIKE '%' + #SearchText + '%'
OR CO.OrderName LIKE '%' + #SearchText + '%') )
Advanced Search
This my sql where clause
WHERE (C.CustomerID =#CustomerID or #CustomerID ISNULL
AND C.Surname =#Surname or #Surname ISNULL
AND C.Name=#Name or #Name ISNULL
AND CA.Street =#Street or #Street ISNULL
AND CA.City =#City or #City ISNULL
AND CO.OrderName =#OrderName or #OrderName ISNULL)
AND ((ModifiedDate BETWEEN ISNULL(convert(varchar,#FromDate,101),'01/01/1901')
AND ISNULL(convert(varchar,#ToDate,101),'12/31/9999'))
How do you do Likes or and or is null in entity framework?
thanks a lot!
For LIKE, you can use Contains, StartsWith, or EndsWith. For IS NULL, use == null.
Example:
var list = from p in Products
where (p.Description.Contains("v") && p.Description.StartsWith("C"))
|| p.MFRCode == "TOYOTA"
|| p.Universal == null
select p;
will cause EF to generate this SQL:
SELECT
[Extent1].[MFRCode] AS [MFRCode],
[Extent1].[MFRProductID] AS [MFRProductID],
[Extent1].[Universal] AS [Universal],
[Extent1].[Description] AS [Description]
FROM [dbo].[Products] AS [Extent1]
WHERE (([Extent1].[Description] LIKE N'%v%') AND ([Extent1].[Description] LIKE N'C%')) OR (N'TOYOTA' = [Extent1].[MFRCode]) OR ([Extent1].[Universal] IS NULL)
and produce these results:
Edit
I used LINQPad to generate these results. It's a great tool, free to use (there's an option to purchase an Intellisense feature), and definitely worth a look if you'd like to experiment with different LINQ queries and see the SQL that EF is generating (it's good for general LINQ experimentation and quickly trying out simple code as well).
int? customerID = null;
string surname = "abc";
var q = from c in oc.Customers
where (customerID == null || c.CustomerID == customerID)
&& (surname == null || c.Surname == surname)
select c;