How to search using LIKE including whitespaces and only spaces? - mysql

I have a recipe table containing id,recipe_name,recipe_ingredients fields.I am joining these tables in order to search a particular recipe.
My question:"How can i search Veg Noodles, Easy Noodles etc using just "noodles" in like query?
My query:
SELECT r.*,l.*,CASE WHEN (COUNT(l.recipe_id)=0)THEN 0 ELSE 1 END as like_status,
u.name,u.user_image,u.location,u.contact_no,COUNT(l.recipe_id) as total_likes
FROM recipe as r
LEFT JOIN likes as l ON r.recipe_id = l.recipe_id
LEFT JOIN user_detail as u ON r.user_id = u.user_id
WHERE r.recipe_name LIKE '%$search%'
Above query of mine returns only the data containing "noodles".I have tried replacing spaces using replace in above query,but not a luck.So how can I search using LIKE including all whitespaces and spaces?

We can use like these
select * from table_name where table_name like "%Veg Noodles%"
These SQL is useful i think
Thank you.

Related

mysql check multiple column in on statement

I am stuck in 1 left join query in which I want to check multiple columns in on statement.
By default in the database, some column is null which I want to check in the on statement.
Now the issue is when I run a query using the OR operator it only runs the 1st condition and the rest are skipped.
If I use AND operator it throws an error.
So is there any way to get data from multiple conditions?
Here is my query:
$data = "SELECT
b.book_name, b.book_id,
b.cats_id, b.cats_id1,
b.cats_id2, b.cats_id3,
b.cats_id4, b.cats_id5,
b.cats_id6,
b.book_rating,
b.book_author,
b.book_stock,
b.book_publisher,
b.book_front_img,
b.book_status,
p.publisher_id,
p.publisher_name,
a.author_id,
a.author_name,
cat.cats_id,
cat.cats_name,
cat.cats_status
FROM
`books` AS b
LEFT JOIN `publisher` AS p
ON b.book_publisher = p.publisher_id
LEFT JOIN `author` AS a
ON b.book_author = a.author_id
LEFT JOIN categorys As cat
ON b.cats_id = cat.cats_id
OR b.cats_id1 = cat.cats_id
OR b.cats_id2 = cat.cats_id
OR b.cats_id3 = cat.cats_id
OR b.cats_id4 = cat.cats_id
OR b.cats_id5 = cat.cats_id
OR b.cats_id6 = cat.cats_id
GROUP BY
b.book_name
HAVING
cat.cats_name = '$search_data'
AND b.book_status = 1
ORDER BY
$sorting
LIMIT $offset, $page_limit"
You probably don't have more than one author displayed for your multi-author books either. You are misusing MySQL's notorious nonstandard extension to GROUP BY.
To troubleshoot this kind of query, disable that extension with SET sql_mode = CONCAT_WS(',',##sql_mode, 'ONLY_FULL_GROUP_BY'), then try your query again. You'll need more terms in your GROUP BY clause.
It looks like each books row has multiple category id columns. And it looks like you want to display information from your categorys table for each of them.
Use GROUP BY b.book_id, p.publisher_id, a.author_id, cats.cats_id to prevent MySQL's bizarro handling of GROUP BY from concealing your data.
I must add this: your multiple books.cats_id columns are not the SQLish way to handle your many-to-many relationship between books and categories. In the parlance of our trade, your books table is denormalized.
What you want is a new table called books_categorys with two columns, book_id and cats_id. It's called a join table. When a row is present in that table, it means a particular book is in a particular category. It's the SQLish way of handling a setup where each book can be in zero or more categorys. Here's an explanation. MySQL join many to many single row
Then you remove all the cats_id columns from books, and retrieve the categories like this.
Then you do something like this SELECT to get the categories.
SELECT books.id, books.name,
categorys.cats_id, categorys.cats_name, categorys.cats_status
FROM books
JOIN books_categorys ON books.book_id = books_categorys.book_id
JOIN categorys ON books_categorys.cats_id = categorys.cats_id
``

Searching SQL database and CONCAT data

New to php and sql so i will try to explain:
I have a SEARCH field in PHP and i am trying to search by 'ProposalName' that match with what the user enters.
This prints out fine:
SELECT
rec_proposal.ProposalID,
ProposalName,
Status,
researcher.FirstName,
researcher.LastName,
reviewer.FirstName as revFirstName,
reviewer.LastName as revLastName,
reviewer.UserID as revUserID,
review.ReviewDate as revDate,
rec_proposal.DateSubmitted
FROM rec_proposal
INNER JOIN User AS researcher
ON rec_proposal.userid = researcher.UserID
LEFT JOIN review
ON rec_proposal.ProposalID=review.ProposalID
LEFT JOIN User as reviewer
ON review.UserID=reviewer.UserID
But now using all the columns I need the above code to do something like this
SELECT * FROM rec_proposal WHERE CONCAT (ProposalName) LIKE'%test%'
SO if user enters the word 'test' you would see ProposalName that contains the words test
Just add your WHERE clause, it should work. And as scaisEdge noted in their comment, you don't need CONCAT() if you are just evaluating a single column :
SELECT
rec_proposal.ProposalID,
ProposalName,
Status,
researcher.FirstName,
researcher.LastName,
reviewer.FirstName as revFirstName,
reviewer.LastName as revLastName,
reviewer.UserID as revUserID,
review.ReviewDate as revDate,
rec_proposal.DateSubmitted
FROM rec_proposal
INNER JOIN User AS researcher
ON rec_proposal.userid = researcher.UserID
LEFT JOIN review
ON rec_proposal.ProposalID=review.ProposalID
LEFT JOIN User as reviewer
ON review.UserID=reviewer.UserID
WHERE rec_proposal.ProposalName LIKE '%test%'

How to search in a two tables with RLIKE where one table can be empty

I have the following query:
SELECT p.partid, pp.partno, p.descr
FROM part p, partcreditor pc, partcreditor_partno pp
WHERE p.partid = pc.partid
AND pc.creditorid = pp.creditorid
AND p.partid = pp.partid
AND (p.partid RLIKE '$val' OR pp.partno_search RLIKE '$val' OR p.descr RLIKE '$val')
For each seperate search term that the user inputs another line like the last line in the query gets added.
This query works very fast but the problem is that table partcreditor does not have a record for table part so than nothing gets returned. So I tried rewriting the query with left joins but this made the query very slow.
Any solutions?
Theres no getting around it... You need left joins.
However, try moving the criteria up into the join condition, like so:
SELECT p.partid, pp.partno, p.descr
FROM part p
JOIN partcreditor pc ON p.partid = pc.partid
LEFT JOIN partcreditor_partno pp ON p.partid = pp.partid
AND pc.creditorid = pp.creditorid
AND pp.partno_search RLIKE '$val'
WHERE p.partid RLIKE '$val'
OR p.descr RLIKE '$val'
It's a little odd that you're doing an RLIKE on an id column.
Since you're doing text searching, you will find that using a FULLTEXT index will improve performance astoundingly.

MySQL joins where one field has surrounding tags to be stripped

I have two MySQL tables, articoli and tabtranslations, and the join between them should be trivial
SELECT CodArt, Translation FROM articoli LEFT JOIN tabtranslations ON articoli.CodArt = tabtranslations.Chiave
BUT, while articoli.CodArt ha simple strings (A001, BS15, etc..), field tabtranslations.Chiave is filled with surronding tags like <CODART>A001</CODART>, <CODART>BS15</CODART> thus overcomplicating joins - and I cannot modify it...
Well, is there a way I can solve this problem? Thanks
A quick and dirty soulution would be like this:
SELECT CodArt, Translation
FROM
articoli LEFT JOIN tabtranslations
ON CONCAT('<CODART>', articoli.CodArt, '</CODART>') = tabtranslations.Chiave
I don't know, but maybe this will work?
SELECT CodArt, Translation
FROM articoli
LEFT JOIN tabtranslations ON tabtranslations.Chiave LIKE '%' + articoli.CodArt + '%'
Just in case 'CODART' isn't the only possible tag, you may want to use REGEXP in the join predicate, like so:
select *
from codart c
left join tabtranslations t
on t.chiave REGEXP CONCAT('<.*>', c.codart, '</.*>');
Here is a sample fiddle for you to try it out: http://sqlfiddle.com/#!9/d6c04/1

Tricky SQL including outer join and case

I use data from http://geonames.org. The table structure is as follows:
GN_Name 1 - 0:N GN_AlternateName
They are linked on:
(PK)GN_Name.GeoNameId == (FK)GN_AlternateName.GeoNameId
GN_Name is the main table containing all place names.
GN_AlternateName contains names in other languages if any.
EX:
GN_Name.Name - Stockholm
GN_AlternateName.AlternateName - Estocolmo (if IsoLanguage=="es")
Rules:
I want to use GN_AlternateName.AlternateName if it exists for the specified language and if it starts with the search string.
If not, i want to use GN_Name.Name if it starts with the search string.
I want GeoNameId to be unique.
Basically I could outer join in first record only, but that seemed to decrease performance.
I've got the following SQL (basically modified SQL from a LINQ query). The problem is that it only finds 'Estocolmo' if search string starts with "stock". "estoc" yields nothing.
select
distinct(n.GeoNameId) as Id,
an.IsoLanguage,
CASE WHEN (an.AlternateName like N'estoc%')
THEN an.AlternateName
ELSE n.Name
END AS [The name we are going to use]
from GN_Name as n
LEFT OUTER JOIN GN_AlternateName as an
ON n.GeoNameId = an.GeoNameId
AND 'es' = an.IsoLanguage
WHERE n.Name like N'estoc%'
UPDATE
Thanks Rahul and Lee D.
I now have the following:
select
distinct(n.GeoNameId) as Id,
an.IsoLanguage,
CASE WHEN (an.AlternateName like N'estoc%')
THEN an.AlternateName
ELSE n.Name
END AS [The final name]
from GN_Name as n
LEFT OUTER JOIN GN_AlternateName as an
ON n.GeoNameId = an.GeoNameId
AND 'es' = an.IsoLanguage
WHERE (n.Name LIKE N'estoc%' OR an.AlternateName LIKE N'estoc%')
This performs LIKE twice on an.AlternateName. Is there any way i could get rid of on LIKE clause?
UPDATE 2
Andriy M made a nice alternative query using COALESCE. I changed it a little bit and ended up with the following:
SELECT Id, LocalisedName
FROM (
SELECT
n.GeoNameId AS Id,
an.IsoLanguage,
COALESCE(an.AlternateName, n.Name) AS LocalisedName
FROM n
LEFT JOIN GN_AlternateName AS an ON n.GeoNameId = an.GeoNameId
AND IsoLanguage = 'es'
) x
WHERE LocalisedName LIKE 'estoc%'
This query does exactly what i am looking for. Thanks!
Here's a probable solution of the problem, which uses a slightly different apporach:
SELECT Id, LocalisedName
FROM (
SELECT
n.GeoNameId AS Id,
an.IsoLanguage,
COALESCE(an.AlternateName, n.Name) AS LocalisedName
FROM GN_Name AS n
LEFT JOIN GN_AlternateName AS an ON n.GeoNameId = an.GeoNameId
AND IsoLanguage = 'es'
) x
WHERE LocalisedName LIKE 'estoc%'
(Changed it based on your update.)
If I understand correctly, in your example the value 'Estocolmo' is in the GN_AlternateName.AlternateName column, so would be filtered out by the where clause which only looks at GN_Name.Name. What if you change the last line of SQL to:
WHERE n.Name LIKE N'estoc%' OR an.AlternateName LIKE N'estoc%'
I'm assuming 'estoc%' is your search string.
I guess you need to modify the WHERE clause to check in GN_AlternateName table as well
WHERE n.Name like N'estoc%' OR an.AlternateName like 'N'estoc%'