MySQL: NOT LIKE - mysql

I have these text in my db,
categories_posts
categories_news
posts_add
news_add
And I don't want to select the rows with categories, I use a query something like this,
SELECT *
FROM developer_configurations_cms
WHERE developer_configurations_cms.cat_id = '1'
AND developer_configurations_cms.cfg_variables LIKE '%parent_id=2%'
AND developer_configurations_cms.cfg_name_unique NOT LIKE '%categories%'
but it returns these two in the output as well...
categories_posts
categories_news
How can I ignore them in my query?
Thanks.

categories_posts and categories_news start with substring 'categories_' then it is enough to check that developer_configurations_cms.cfg_name_unique starts with 'categories' instead of check if it contains the given substring. Translating all that into a query:
SELECT *
FROM developer_configurations_cms
WHERE developer_configurations_cms.cat_id = '1'
AND developer_configurations_cms.cfg_variables LIKE '%parent_id=2%'
AND developer_configurations_cms.cfg_name_unique NOT LIKE 'categories%'

I don't know why
cfg_name_unique NOT LIKE '%categories%'
still returns those two values, but maybe exclude them explicit:
SELECT *
FROM developer_configurations_cms
WHERE developer_configurations_cms.cat_id = '1'
AND developer_configurations_cms.cfg_variables LIKE '%parent_id=2%'
AND developer_configurations_cms.cfg_name_unique NOT LIKE '%categories%'
AND developer_configurations_cms.cfg_name_unique NOT IN ('categories_posts', 'categories_news')

Related

Difference between "and not ()" and "and (... not like ...)"?

I receive completely different results from my query depending on where I put NOT. Can you please explain the difference?
Below is the first way:
SELECT COUNT(*)
FROM [table]
WHERE condition = 'N'
AND (Category NOT LIKE '%str1%'
OR Category NOT LIKE '%str2%'
OR Category NOT LIKE '%str3%')
The second way:
SELECT COUNT(*)
FROM [table]
WHERE condition = 'N'
AND NOT (Category LIKE '%str1%'
OR Category LIKE '%str2%'
OR Category LIKE '%str3%')
Both seem the same for me.
You need to change the operators within the parentheses to AND, if you add NOT, which will impact adversly all the terms within parentheses, such as
SELECT count(*)
FROM[table]
WHERE condition = 'N'
AND NOT (Category LIKE '%str1%'
AND Category LIKE '%str2%'
AND Category LIKE '%str3%')
A OR B equals NOT ( A AND B ) in Boolean logic
I just want to note that you can simplify this logic:
WHERE condition = 'N' AND
Category NOT REGEXP 'str1|str2|str3'
This eliminates issues with parentheses and the placement of the NOT.

How to query for multiple wildcard operators in a sigle query?

In the below code everything is fine but my values are not matching to what they should be, I want to understand if I am using the wildcard operator correctly, that is I want to pull multiple values from the column post_evar10 using wildcard. I get correct results when I use one wildcard operator at a time.
where (post_evar10 like 'states:%' or
post_evar10 like 'www:' or
post_evar10 like 'local:%' or
post_evar10 like 'learn:%')
create table temp.MS_Adobe_Discover1
Select
concat(month(date_time),'/',day(date_time),'/', year(date_time)) as Date,
post_evar10,
count(page_event) as Pageviews,
count(distinct concat(post_visid_high, post_visid_low)) as UniqueVisitors
from adobe_hits
where (post_evar10 like 'states:%' or
post_evar10 like 'www:' or
post_evar10 like 'local:%' or
post_evar10 like 'learn:%')
and page_event like '0'
and exclude_hit like '0'
and hit_source not in (5,7,8,9)
group by Date, post_evar10;
Sample Result:
Please see the image.enter image description here
You can also use left which might be faster on some systems and some data sets --- like this:
where (left(post_evar10,7) = 'states:' or
post_evar10 = 'www:' or
post_evar10,6) = 'local:' or
post_evar10,6) = 'learn:')
if you don't care about the : in states: this would def. be faster
where (left(post_evar10,6) in ('states','local:','learn:') or
post_evar10 = 'www:' )

Multiple AND/OR logic messed up

I need to get my data when userID=4 and status= In use or Pending or Deleted.
But I am getting extra data not sure why.
I am getting extra data from
userid=3
MySQL query is:
SELECT * FROM `registered_bicycle`
WHERE (`userID`='4')
AND (`status`='In Use')
OR (`status`='Pending')
OR (`status`='Deleted')
You have to use (...) arround the OR Statements:
SELECT * FROM `registered_bicycle`
WHERE (`userID`='4')
AND (`status`='In Use'
OR `status`='Pending'
OR `status`='Deleted')
or use IN function
Put the part after the AND between braces, otherwise it will return data with userID=4 OR status=xy
SELECT * FROM `registered_bicycle` WHERE (`userID`='4') AND (
(`status`='In Use')
OR (`status`='Pending')
OR (`status`='Deleted')
)
Try this instead:
SELECT
*
FROM
`registered_bicycle`
WHERE `userID` = '4'
AND `status` IN('In Use','Pending','Deleted')
Your and/or logic got mixed up because of lacking proper parentheses. Also you can use IN instead of multiple OR clauses.
Btw, IN is just the short form of OR
try this:
SELECT * FROM `registered_bicycle`
WHERE (`userID`='4')
AND `status`IN('In Use','Pending','Deleted')

MYSQL - Select selecting rows where another column equals something

I have this query:
select feature_requests.*,
from feature_requests
where feature_requests.status in ('open','closed','indevelopment')
I also have another status - denied.
I need to also select all rows with status denied but another column on my features request table must equal something.
So something that does this:
select feature_requests.*,
from feature_requests
where feature_requests.status in ('open','closed','indevelopment','denied') and
if status = denied, instance_id = ?
Not sure of the correct syntax. Thanks for any help :)
The WHERE clause is the correct place to put these kind of conditions, but with a few differences:
SELECT `fr`.*
FROM `feature_requests` fr
WHERE (`fr`.`status` IN ('open','closed','indevelopment')) OR
((`fr`.`status` = 'denied') AND (`fr`.`instance_id` = ?))
P.S - Notice I'm using an alias for feature_requests called fr instead of writing the whole name again and again. And You don't have to write its name at all because you're using only one table in your query, but I would still use it because it reduces the chances of future mistakes.
For further reading - SELECT Syntax
From rusty memory, you're probably wanting something like this
SELECT feature_requests.* FROM feature_requests
WHERE feature_requests.status IN ('open', 'closed', 'indevelopment') OR
(feature_requests.status='denied' AND instance_id = ???)
What you have right now is pretty close.
http://www.tutorialspoint.com/sql/sql-and-or-clauses.htm
This should work:
SELECT
feature_requests.*
FROM
feature_requests
WHERE
feature_requests.status IN ('open','closed','indevelopment') OR (
feature_requests.status='denied' AND
instance_id=?
)
This can also be written without listing the table name over and over if it is the only table that you are using like this:
SELECT
*
FROM
feature_requests
WHERE
status IN ('open','closed','indevelopment') OR (
status='denied' AND
instance_id=?
)
When using AND and/or OR in your where clause please also remember to use parenthesis ( ) to show your actual meaning even when you know what takes precedence between the AND and OR. For more information on precedence of operators with MySQl Example:
color=blue AND shape=circle OR type=ball
means
(color=blue AND shape=cirlce) OR type=ball
but could easily be misinterpreted as
color=blue AND (shape=circle OR type=ball)

Query is Giving Wrong Results

SELECT `bio_community_groups`.`id`, `bio_community_groups`.`name`, `bio_community_groups`.`description`, `bio_community_groups`.`members`
FROM `bio_community_groups`
WHERE `bio_community_groups`.`category_id` = '1'
AND `bio_community_groups`.`name` LIKE '%rock%'
OR `bio_community_groups`.`description` LIKE '%rock%'
Problem: there isn't group with ID = 1, but anyway... it gives me all groups where name or description is like '%rock%'.
Maybe brackets may help me? Where should I put them?
Perhaps this is what you might be looking for :
SELECT `bio_community_groups`.`id`, `bio_community_groups`.`name`, `bio_community_groups`.`description`, `bio_community_groups`.`members`
FROM `bio_community_groups`
WHERE
( `bio_community_groups`.`category_id` = '1' )
AND
( `bio_community_groups`.`name` LIKE '%rock%'
OR `bio_community_groups`.`description` LIKE '%rock%' );
In your original query, you will get results satisfying :
`bio_community_groups`.`description` LIKE '%rock%
whatever the category_id may be .
AND precedes OR in MySQL. so your query is like (bio_community_groups.category_id = '1'
AND bio_community_groups.name LIKE '%rock%')
OR (bio_community_groups.description LIKE '%rock%). Just place the appropriate brackets to resolve this