mySQL WHERE.. OR query - mysql

I want to execute a query on a database to select all rows in the 'Event' table where the 'about' section has any of the following words in it: strokestown, arts, day. My query, shown below is only getting rows that have the first word, strokestown in them. How do I make it search for all words?
SELECT *
FROM Event
WHERE about LIKE 'strokestown%'
OR about LIKE 'arts%'
OR about LIKE 'day%';
Thank you for your time!!
Jim

Place the wildcard character, '%', at the start as well as the end of the your search terms:
SELECT *
FROM Event
WHERE about LIKE '%strokestown%'
OR about LIKE '%arts%'
OR about LIKE '%day%';

SELECT * FROM Event
WHERE about LIKE '%strokestown%'
OR about LIKE '%arts%'
OR about LIKE '%day%'
Put a % before and after the keywords.

You can make this smaller like this: SELECT * FROM Event WHERE about REGEXP '(strokestown|arts|day)'

SELECT * FROM Event WHERE about LIKE '%strokestown%' OR about LIKE '%arts%' OR about LIKE '%day%';

Related

Query to select data from mysql with specific condition

In MySQL we can use this code to select rows with ID numbers (or anything else) between a list:
SELECT * FROM TABLENAME WHERE prophrases IN (1,2,3,4,5,6)
Thats OK! but if we need to search a number in a Field's value, what we can do?
For example, I have a table with a field, named 'prophases' and I saved data like this:
rowid / prophases
1 / 1,2,3,4,5,6,7,8
2 / 6,5,2,7,9,2
now, i need to check if a number like 6 is in prophrases in row #1 or not!
what can i do for that?
something like this but in correct form!
SELECT * FROM TABLENAME WHERE 6 IN prophrases
you should just use FIND_IN_SET()
SELECT rowid
FROM TABLENAME
WHERE FIND_IN_SET('6', prophrases)
This will work if you are only dealing with single digits
select * from tablename where prophrases like '%6%'
This will work if you are going to have number with more than one digit and there are no spaces between commas.
select * from tablename where prophrases like '%,6,%' or prophrases like '6,%' or prophrases like '%,6'
You need to use a like statement
SELECT * FROM TABLENAME WHERE prophrases LIKE '%6%'
However if you have multiple digit numbers that could cause some unexpected results, so you might have to amend it like
SELECT * FROM TABLENAME
WHERE prophrases LIKE '%,6,%'
OR prophrases LIKE '6,%'
OR prophrases LIKE '%,6'
The first part matches 6 in between commas, the second one matches a 6 at the beginning followed by a comma, the third matches a comma followed by a six a the end.
Storing data like that is not the best way of doing it. You are probably better off having another table that has a one-to-many relationship.

mysql like query with special character '

I wrote query for filter data using name and wrote following query
SELECT * FROM (`abc`) WHERE (name LIKE "%test\'!&##$\%-(3)\_er%")
It should return records which has name start with text "test"
but it will not instead of if I modify query like
SELECT * FROM (`abc`) WHERE (name LIKE "%test%\'!&##$\%-(3)\_er%")
then it will give result. Why it is not give result with first query?
Is there any other way to do this?
The % is the wildcard in the query.
So %test means everything that ends with test.
and test% means everything that begins with test.
and %test% means everything with test in it.
Simpy change your query to
SELECT * FROM (abc) WHERE (name LIKE "test%")
If you want records that start with test, simply use
SELECT * FROM (`abc`) WHERE (name LIKE "test%")

whole phrase priority searching

we are implementing a search application
we have implemented a exact word search by the following sql query
SELECT *
FROM jreviews_content
WHERE jr_produits REGEXP '[[:<:]]ryan[[:>:]]'
which works well now we have another requirement
If the user enter a word "Cabaret Mile-End" various result comes up which has Cabaret and Mile-End in it but the row that has whole phrase in it comes at the lastResult.So i want a whole phrase priority wise searching.
As my comment said:
Try using the LIKE structure as follows
SELECT *
FROM jreviews_content
WHERE jr_produits LIKE '%Cabaret Mile-End%'
UNION ALL
SELECT *
FROM jreviews_content
WHERE jr_produits REGEXP '[[:<:]]Cabaret Mile-End[[:>:]]'
I dont know what you put in the last query so I put the value just in between.

"Merge" IN and LIKE operator in Mysql

I've a problem and I'm guessing if there's a better way to achieve my goal. I've this query in Mysql to retrieve some rows:
SELECT *
FROM table
WHERE field IN ('V','S','G','B')
What I would like to do is to run a query that retrieve the rows where the field has value LIKE those in the IN list. I know that the trivial way is to use this query:
SELECT *
FROM table
WHERE field LIKE '%V%' OR field LIKE '%S%' OR
field LIKE '%G%' OR field LIKE '%B%'
What I want to know is there's an operator that do this or at least, if that operator does not exist, a better way to write the query.
Thanks to everyone that will help me.
Put the values in a table (Params) then use
SELECT *
FROM table
WHERE EXISTS (
SELECT *
FROM Params
WHERE table.field LIKE '%' + Params.col + '%'
);
Consider also putting the wildcard characters into the values in the Params table.

MySql Select where like, excluding?

I have a bunch of text fields and I need to search for #& but I want to not include #&39; as almost every row has that.
Is there a way to find #& blah blah; and not get every ' ?
I tried doing something like
SELECT * FROM `content_type_mobile_event` WHERE `field_movie_desc_value` RLIKE "^(&#)";
But that didn't work
Try:
SELECT *
FROM `content_type_mobile_event`
WHERE `field_movie_desc_value`
LIKE '&#%'
AND `field_movie_desc_value`
NOT LIKE ''%';
This will return the records whose value on the column field_movie_desc_value starts with &#, but does not start with '.
Did you simply try:
SELECT * FROM `content_type_mobile_event` WHERE `field_movie_desc_value` like "%#&" AND
WHERE `field_movie_desc_value` not like "#&39"
Can you just do a LIKE and a NOT LIKE?
SELECT * FROM `content_type_mobile_event`
WHERE `field_movie_desc_value` NOT LIKE '%#&39;%'
AND `field_movie_desc_value` LIKE '%#&'";