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
Related
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.
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:' )
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)
I have query with a bunch of LIKE statenements. I want to optizimze/explain the query, which runs fine. However if I copy this:
SELECT a.*, p.ek, p.vk, p.waehrung, p.onlinepreis
FROM arts a USE INDEX (i_aktiv, i_iln, i_marke )
LEFT JOIN pls p ON
p.iln = a.iln
LEFT JOIN fbs zu
ON a.farbe = zu.farbe
WHERE a.aktiv = "ja"
AND (
a.artikelbezeichnung LIKE ?
OR a.artikelbezeichnung_lang LIKE ?
OR ( a.farbe LIKE '%decke%'
OR zu.systemfarbe LIKE '%decke%'
OR zu.systemfarbe_en LIKE '%decke%'
)
)
)
...
As parameters I want to add '%decke%' but MySQL keeps throwing an error, if I use the %%.
Any idea what I'm doing wrong?
The error I'm getting is:
you have an error in your SQL-syntax.... check near... %'%'decke%' at OR ( a.farbe LIKE '%decke%' ...
For wildcarded LIKE comparisons, you have to fake it:
WHERE somefield LIKE CONCAT('%', ?, '%')
wildcarded 'likes' are one place that parameterized queries fall flat on their faces.
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')