MySql Select where like, excluding? - mysql

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 '%#&'";

Related

Retrieve any name starting with letters 'Ai' in SQL

All I want to do is retrieve the names of the people who start with "ma".
Eg Matt, Mathers, Mac.
First, two letters have to be 'ma'.
Have a look at string comparisons functions.
SELECT *
FROM YOUR_TABLE
WHERE NAME LIKE 'MA%';
SELECT *
FROM YOUR_TABLE
WHERE NAME LIKE 'MA%'
There can be spaces at the beginning of the name just like ' Matthew'. This would fix it:
SELECT *
FROM YOUR_TABLE
WHERE ltrim(NAME) LIKE 'MA%'

Write a SQL query to list all employee names and jobs, whose job title includes M or P?

I have to display the names of jobs which contains letters m or p . does'nt matter the position of letters where they are at .they can be either at starting or middle or what ever position.
thank you
Hi you can use like query for this type result-
SELECT column_name FROM table_name WHERE job like ('%m%') OR job like ('%p%');
Try this one:
select name,job from employee where jobtitle like '%M%' or '%P%'
As an alternative to LIKE, we can use REGEXP here:
SELECT job
FROM jobs
WHERE job REGEXP 'm|p';
Demo
TRY This one
SELECT * FROM `TABLENAME` WHERE 'JOBNAME' LIKE '%p%' OR 'JOBNAME' LIKE '%m%'

sql select where string matches pattern defined in another table

I have a select statement like this:
Select * from A where name like 'a%' or name like 'b%' or name like 'j%' or name like ... etc
Is it possible to store a%, b%, j% in a table somewhere so I can more easily manage this list and convert my query to something like:
Select * from A where name like (Select * from StringPatternToMatch)
Try this:
SELECT * FROM A
JOIN StringPatternToMatch patt ON A.name LIKE '%' + patt.pattern + '%';
Replace patt.pattern with the name of the column in your StringPatternToMatch
You can do a regexp search instead.
select *
from A where name regexp '^[abjf]';
It's easier query to maintain than a ton of or'd likes.
demo here
'^[abjf]' means match the start of the string (^), followed by any of the characters in the list ([abjf]). It doesn't care what comes after that.
Just add more letters to the list if you find names starting with them.

how to restrict `like` operator in query to start with something

I want to write a query that title start with A or B
is this correct?
I dont want use OR
I want use it in mysql ,
select * from table where title like `[AB]%`
Use REGEXP instead of LIKE:
SELECT * FROM table
WHERE title REGEXP '^[AB]'
DEMO
Or just use a substring:
SELECT * FROM table
WHERE LEFT(title, 1) IN ('A', 'B');
you can do it like
select * from table where title like `A%` OR title like `B%`
another way is to use regular expression
select * from table where title REGEXP '^(A|B)';
This is correct way:
SELECT * FROM table WHERE title LIKE 'A%' or title LIKE 'B%';
What you wrote should work. or you can use,
select *
from table_name
where title LIKE 'A%' OR title LIKE 'B%'
Unfortunately, the LIKE operator in SQL only supports a limited syntax. It doesn't support a regex style syntax.
You have to do divide it into two expressions:
select * from table where (title like 'A%' or title like 'B%')
Note:
In this case the parenthesis are superfluous, but since OR has a lower precedence than AND I think it is good practice to routinely add parenthesis around ORexpressions in SQL.

mySQL WHERE.. OR query

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%';