AND clause on multiple values for same column - mysql

How can I fix this query?
SELECT COUNT(*)
FROM user_notifications
WHERE notification_status = '0'
AND notification_sent_to = ?
AND notification_category = 'ticket reply'
AND notification_category = 'groupdoc'
I need to count result filtering only notification category for ticket reply and groupdoc.
Many thanks for your help.

A particular column can have only one value in a given row. So the following condition can never be True
notification_category = 'ticket reply'AND notification_category= 'groupdoc'
When people think I need rows with value of 'a' and rows with value of 'b', the actual logical operator to use is OR
SELECT COUNT(*) FROM user_notifications WHERE notification_status = '0' AND notification_sent_to=? AND
(notification_category = 'ticket reply' OR notification_category= 'groupdoc')

A column can not have 2 values at same time. I think what you want is an OR
SELECT COUNT(*)
FROM user_notifications
WHERE notification_status = 0 AND notification_sent_to=?
AND (notification_category = 'ticket reply' OR notification_category= 'groupdoc')

Use in:
SELECT COUNT(*)
FROM user_notifications
WHERE notification_status = 0 AND
notification_sent_to = ? AND
notification_category IN ('ticket reply', 'groupdoc');

Related

mySQL Inclusive search on a flat table

I have the below SQL table, and i'm wondering if it's possible to have an inclusive search with this table format? Meaning if someone were to search for SSNU and ACCOUNT_NO it would only return the data if the CASE ID has an SSNU and ACCOUNT_NO that match the inputs.
I've tried a where clause like the below:
AND (a.KEY = "ACCOUNT_NO" AND a.VALUE = "XXXXXX")
AND (a.KEY = "GPID" AND a.VALUE = "XXXXX")
but have not had any luck.
Do you want to prioritize the row whose key is 'ACCOUNT_NO', and fallback on 'GPID'? If you want just one row, then you can use order by and limit:
select *
from mytable a
where a.key in ('ACCOUNT_NO', 'GPID') and a.value = 'XXXXXX'
order by a.key = 'ACCOUNT_NO' desc
limit 1
The order by clause puts first the row whose key is 'ACCOUNT_NO', if any. Note that I factorized the condition on VALUE, since it is common to both conditions.
If by "person" you mean "case id", then I think you want aggregation:
select case_id
from t
where (a.KEY = 'ACCOUNT_NO' AND a.VALUE = 'XXXXXX') or
(a.KEY = 'GPID' AND a.VALUE = 'XXXXX')
group by case_id
having count(distinct a.key) = 2;
This returns cases that have both the key/value pairs.

Two count request with double where and from in one query

I'm trying to count the number of occurrences of two values "1" and "2" in the "ticket" column, but I get an error when I add another line from "where" and "from".
"ticket" is a column, "tickets" is a table.
SELECT
COUNT(ticket) as child FROM tickets WHERE ticket = 1,
COUNT(ticket) as adult FROM tickets WHERE ticket = 2
GROUP by ticket
SELECT
COUNT(if(ticket = 1,1,NULL)) as child,
COUNT(if(ticket = 2,1,NULL)) as adult FROM tickets where ticket in (1,2)
GROUP by ticket
or
SELECT
sum(if(ticket = 1,1,0)) as child,
sum(if(ticket = 2,1,0)) as adult FROM tickets where ticket in (1,2)
GROUP by ticket
try like this
Use conditional aggregation:
SELECT SUM(t.ticket = 1) as t1,
SUM(t.ticket = 2) as t2
FROM tickets t;
You're looking for conditional aggregation, like so:
SELECT COUNT(IF(ticket=1, 1, NULL) AS child
, COUNT(IF(ticket=2, 1, NULL)) AS adult
FROM tickets
;
COUNT only counts non-null values.
Your query is syntactically invalid, try this one:
Select count(*)
, ticket
from tickets
where ticket In (1,2)
Group by ticket
;
Try this out and let me know in case of any queries.
SELECT SUM(ticket = 1) as t1,
SUM(ticket = 2) as t2
FROM tickets
group by tickets;

Multiple joins with ordered by count ignores where conditions

I am trying to only get rows from video_index that belongs to a specific category from category_video_rel and order the result by COUNT of view count. This is the query I'm using:
SELECT
COUNT(DISTINCT view_count.id) AS count,
view_count.remove,
view_count.video_id,
video_index.id AS video_id,
video_index.active,
video_index.remove,
video_index.title AS title,
video_index.date_published AS date_published,
category_video_rel.active,
category_video_rel.remove,
category_video_rel.video_id AS cv_video_id,
category_video_rel.category_id AS category_id
FROM
view_count JOIN video_index
ON view_count.video_id = video_index.id,
category_video_rel JOIN video_index AS v
ON category_video_rel.video_id = v.id
WHERE
view_count.remove = '0' AND
video_index.active = '1' AND
video_index.remove = '0' AND
video_index.date_published <= '$current_time' AND
category_video_rel.category_id = '$category_id' AND
category_video_rel.active = '1' AND
category_video_rel.remove = '0'
GROUP BY
video_index.id
ORDER BY
count DESC
The problem is it outputs all the rows from video_index with a view count higher than 0 regardless of the category. Basically, it's ignoring "category_video_rel.category_id = '$category_id'" in the WHERE condition.
I have no idea what I'm doing wrong, please help.
Your FROM clause is mixing old style joins and new style joins
Instead try:
FROM
view_count JOIN video_index
ON view_count.video_id = video_index.id
JOIN category_video_rel
ON category_video_rel.video_id = video_index.id

Conditionally select from one table or another in MySQL

In MySQL, how do I select from another table if a foreign key is set?
What I'm trying to do is select Fields.value if Fields.value_id isn't set, otherwise select Values.value from Values where Value.id is equal to Fields.value_id.
My tables:
Fields:
id | value | value_id
Values:
id | value
What's wrong with my code here?
Code:
SELECT CASE
WHEN Field.value_id = NULL OR Field.value_id = ""
THEN Field.value
ELSE
Value.value
FROM values as Value
WHERE (Field.value_id = Value.id)
One syntax error is that you are missing the end in the case. I also think you want a left join between the tables. My best guess given the available information is this:
SELECT (CASE WHEN f.value_id = NULL OR f.value_id = ''
THEN f.value
ELSE v.value
END)
FROM fields f left join
values v
on f.value_id = v.id;

How to get individual count using this MySql Query

I have table and this table contain result column with some entries. I just wanted to know how to get individual count these entries using MySql Query like this (see result required)...
also
(see the result column image and query) or helps are definitely appreciated
Result Column Image
Query
SELECT cpd.result FROM cron_players_data cpd
WHERE cpd.`status` = '1'
AND (cpd.`result` = '1' OR cpd.`result` = '2')
AND cpd.`player_id` = '81'
Result Required
result count
1 2
2 6
SELECT cpd.result, count(*) FROM cron_players_data cpd
WHERE cpd.`status` = '1'
AND (cpd.`result` = '1' OR cpd.`result` = '2')
AND cpd.`player_id` = '81' group by cpd.`result`
SELECT result, COUNT(result) `count` FROM
(
SELECT cpd.result result
FROM cron_players_data cpd
WHERE cpd.`status` = '1'
AND (cpd.`result` = '1' OR cpd.`result` = '2')
AND cpd.`player_id` = '81'
) a
GROUP BY result