Return null values IN clause mysql - mysql

Print not found when there is no data found in database. For example in my database I do not have 56443 therefore it should print 'not found'
SELECT uid, (CASE WHEN (u.uid = null) THEN 'not found' ELSE 'found' END) as result
FROM (SELECT uid
FROM users
WHERE uid IN (1,2,56443,3)) as u;
Getting result as follows
+--------+--------+
| uid | result|
+--------+--------+
| 1 | found |
| 2 | found |
| 3 | found |
+--------+--------+
I am also expecting not found row with 56443

You need to use a different approach. You will need to create a inline view with all the values using the UNION ALL, and then left join it with the users table:
SQL Fiddle
Query 1:
SELECT a.uid, (CASE WHEN (u.uid is null) THEN 'not found' ELSE 'found' END) as result
FROM (select 1 as UID FROM dual
UNION ALL
select 2 as UID FROM dual
UNION ALL
select 56443 as UID FROM dual
UNION ALL
select 3 as UID FROM dual) as a
LEFT JOIN users u on a.uid = u.uid
[Results]:
| UID | result |
|-------|-----------|
| 1 | found |
| 2 | found |
| 3 | found |
| 56443 | not found |

That is because you are comparing a value with null aka. unknown. Always use the IS operator when comparing to null values.
CASE WHEN (u.uid is null) THEN 'not found' ELSE 'found' END) as result
Try this instead (updated answer):
SELECT u2.uid, (CASE WHEN (u1.uid is null) THEN 'not found' ELSE 'found' END)
as result
FROM users u1
RIGHT JOIN
(select 1 as uid union all
select 2 as uid union all
select 3 as uid union all
select 56443 as uid
) u2
on u1.uid = u2.uid

Related

JOIN and SUM different statement results (Wordpress-Mailster Database)

After the last update of Mailster (email marketing plugin for wordpress), they have changed the way they store the information about opens, clicks, unsubscribes...
Until now, everything was stored in two databases:
bao_posts: Like any other wordpress post, the information of the
email that is sent was there. (When the post_type = 'newsletter')
bao_mailster_actions: This is where the user's actions with the
email were stored. 1 when it was sent to a person, 2 when they
opened it, 3 when they clicked on it and 4 when they unsubscribed.
And with this query, I could get a table with all the emails and the information of their openings, clicks, unsubscribed...
SELECT bao_posts.post_modified,
bao_posts.ID,
bao_posts.post_title,
COUNT(CASE WHEN bao_mailster_actions.type = 1 then 1 ELSE NULL END) AS Number_People_Reached,
COUNT(CASE WHEN bao_mailster_actions.type = 2 then 1 ELSE NULL END) AS Opens,
COUNT(CASE WHEN bao_mailster_actions.type = 3 then 1 ELSE NULL END) AS Clicks,
COUNT(CASE WHEN bao_mailster_actions.type = 4 then 1 ELSE NULL END) AS Unsubs
FROM bao_posts
LEFT JOIN bao_mailster_actions ON bao_mailster_actions.campaign_id = bao_posts.ID
WHERE bao_posts.post_type = 'newsletter'
GROUP BY bao_posts.ID ;
*Expected result of this query at the end of the post.
Now the problem is that this setting is kept for emails before the update, but it has changed for new ones and now bao_mailster_actions is separated into:
bao_mailster_action_sent
bao_mailster_action_opens
bao_mailster_action_clicks
bao_mailster_action_unsubscribes
I know how to get the count of each of these tables like this:
SELECT bao_mailster_action_sent.campaign_id,
COUNT(bao_mailster_action_sent.count) AS Number_People_Reached
FROM bao_mailster_action_sent
GROUP BY bao_mailster_action_sent.campaign_id;
To get:
campaign_id
Number_People_Reached
9785
300
9786
305
(And so on with each of these 4 new tables).
So what I would like to do would be to join these 4 new queries to the original one. I've been trying to combine different JOINs, but I don't quite understand how to do it.
*Bearing in mind that if an email ID matches in both, I would need it to add up their clicks, opens (or whatever).
The expected outcome would be something like this (the same as the first query but with the aggregate data):
post_modified
ID
post_title
Number_People_Reached
Opens
Clicks
Unsubs
2021-04-29 13:13:03
9785
Prueba email
300
102
30
1
2021-04-30 15:12:01
9786
Segundo email
305
97
56
0
Thanks in advance!
I suggest that you use UNION ALL to join all the tables in a CTE.You can then use this in your query. I have modified the name because we cannot have to records with the same name.
> create table if not exists bao_mailster_action_sent
( campaign_id int,count int);
create table if not exists bao_mailster_action_opens
( campaign_id int,count int);
create table if not exists bao_mailster_action_clicks
( campaign_id int,count int);
create table if not exists bao_mailster_action_unsubscribes
( campaign_id int,count int);
CREATE TABLE if not exists bao_posts(
post_modified date,
ID int,
post_title varchar(50) );
insert into bao_mailster_action_sent values
(1,88),(2,4),(4,6);
insert into bao_mailster_action_opens values
(2,4),(3,5),(4,10);
insert into bao_mailster_action_clicks values
(1,3),(2,3),(4,6);
insert into bao_mailster_action_unsubscribes values
(1,4),(3,5),(4,5);
INSERT INTO bao_posts values
( '2021-03-01',1,'first post'),
( '2021-06-01',2,'second opion'),
( '2021-09-01',3,'third way'),
( '2021-12-01',4,'last post');
WITH bao_mailster_actionsent AS
( SELECT campaign_id,count, 1 type FROM
bao_mailster_action_sent
UNION ALL
SELECT campaign_id,count,2 FROM
bao_mailster_action_opens
UNION ALL
SELECT campaign_id,count,3 FROM
bao_mailster_action_clicks
UNION ALL
SELECT campaign_id,count,4 FROM
bao_mailster_action_unsubscribes)
SELECT bao_mailster_actionsent.campaign_id,
COUNT(bao_mailster_actionsent.count) AS TotalCount,
SUM(bao_mailster_actionsent.count) AS TotalNumber,
'type'
FROM bao_mailster_actionsent
GROUP BY bao_mailster_actionsent.campaign_id,'type' ;
WITH baoMailsterAction AS
( SELECT campaign_id,count, 1 type FROM
bao_mailster_action_sent
UNION ALL
SELECT campaign_id,count,2 FROM
bao_mailster_action_opens
UNION ALL
SELECT campaign_id,count,3 FROM
bao_mailster_action_clicks
UNION ALL
SELECT campaign_id,count,4 FROM
bao_mailster_action_unsubscribes)
SELECT bao_posts.post_modified,
bao_posts.ID,
bao_posts.post_title,
COUNT(CASE WHEN bao_mailster_actions.type = 1 then 1 ELSE NULL END) AS Number_People_Reached,
COUNT(CASE WHEN bao_mailster_actions.type = 2 then 1 ELSE NULL END) AS Opens,
COUNT(CASE WHEN bao_mailster_actions.type = 3 then 1 ELSE NULL END) AS Clicks,
COUNT(CASE WHEN bao_mailster_actions.type = 4 then 1 ELSE NULL END) AS Unsubs
FROM bao_posts
campaign_id | TotalCount | TotalNumber | type
----------: | ---------: | ----------: | ---:
1 | 1 | 88 | 1
2 | 1 | 4 | 1
4 | 1 | 6 | 1
2 | 1 | 4 | 2
3 | 1 | 5 | 2
4 | 1 | 10 | 2
1 | 1 | 3 | 3
2 | 1 | 3 | 3
4 | 1 | 6 | 3
1 | 1 | 4 | 4
3 | 1 | 5 | 4
4 | 1 | 5 | 4
post_modified | ID | post_title | Number_People_Reached | Opens | Clicks | Unsubs
:------------ | -: | :----------- | --------------------: | ----: | -----: | -----:
2021-03-01 | 1 | first post | 1 | 0 | 1 | 1
2021-06-01 | 2 | second opion | 1 | 1 | 1 | 0
2021-09-01 | 3 | third way | 0 | 1 | 0 | 1
2021-12-01 | 4 | last post | 1 | 1 | 1 | 1
db<>fiddle here
I finally got it to work using only the new tables that Mailster created (it seems that finally they did move all the info to the new tables with the update) and with 4 LEFT JOINS.
I leave the code in case someone else finds it useful:
SELECT P.post_modified,
P.ID,
P.post_title,
IFNULL(S.count,0) as 'Total',
IFNULL(O.count,0) as 'Aperturas',
IFNULL(C.count,0) as 'Clicks',
IFNULL(U.count,0) as 'Bajas' from bao_posts as P
LEFT JOIN (select campaign_id, count(DISTINCT subscriber_id) as count from bao_mailster_action_clicks group by campaign_id) as C ON C.campaign_id = P.ID
LEFT JOIN (select campaign_id, count(DISTINCT subscriber_id) as count from bao_mailster_action_opens group by campaign_id) as O ON O.campaign_id = P.ID
LEFT JOIN (select campaign_id, count(DISTINCT subscriber_id) as count from bao_mailster_action_sent group by campaign_id) as S ON S.campaign_id = P.ID
LEFT JOIN (select campaign_id, count(DISTINCT subscriber_id) as count from bao_mailster_action_unsubs group by campaign_id) as U ON U.campaign_id = P.ID
WHERE P.post_type = 'newsletter'
ORDER BY P.post_modified ASC ;
P.S: As I expected, Mailster's support has not helped at all :'(

Select group multiple records by specific condition

I have table:
+----+-------+-------------+
| id | code | value_check |
| 1 | p-01 | OK |
| 2 | p-01 | NOT OK |
| 3 | p-01 | OK |
| 4 | p-02 | OK |
| 5 | p-02 | OK |
| 6 | p-02 | OK |
+----+-------+-------------+
How can I select record which having 'OK' group by code,but if there is one or more 'NOT OK' on value_check then don't need to select
expected result:
code
p-02
i have tried my query can get the result but its very slow
this is my query :
SELECT code FROM table
WHERE code
NOT IN (SELECT code FROM table
WHERE value_check = 'NOT OK' GROUP BY code)
GROUP BY code
any other solution?
Check whether the total count is equal to the count of rows having value as OK using HAVING clause.
Query
select `code` from `your_table_name`
group by `code`
having count(*) = sum(`value_check` = 'OK');
Find a demo here
Try below with conditional aggregation
select code from table
group by code
having sum(case when value_check='NOT OK' then 1 else 0 end)=0
You can try it also with correlated subquery:
demo
SELECT distinct code FROM cte1 a
WHERE NOT exists (SELECT 1 FROM cte1 b where a.code=b.code and val = 'NOT OK')
SELECT DISTINCT x.code
FROM my_table x
LEFT
JOIN my_table y
ON y.code = x.code
AND y.value_check = 'not ok'
WHERE x.value_check = 'ok'
AND y.id IS NULL

MySQL: Select multiple max values from one column

Suppose I got this table:
mytable
+------+-------+
| type | count |
+------+-------+
| red | 4 |
| blue | 3 |
| red | 2 |
| blue | 7 |
+------+-------+
Now I want this back:
+--------+---------+
| maxRed | maxBlue |
+--------+---------+
| 4 | 7 |
+--------+---------+
How can I do this?
This is what I tried
SELECT MAX(count) as maxRed, 0 as maxBlue FROM mytable WHERE type='red'
UNION
SELECT 0 as maxRed, MAX(count) as maxBlue FROM mytable WHERE type='blue'
but it does not quite work and only results
+--------+---------+
| maxRed | maxBlue |
+--------+---------+
| 4 | 0 |
| 0 | 7 |
+--------+---------+
For your query to work you only needed to wrap it with another query:
SELECT MAX(maxRed) as maxRed, MAX(maxBlue) as maxBlue
FROM(
SELECT MAX(count) as maxRed, 0 as maxBlue FROM mytable WHERE type='red'
UNION
SELECT 0 as maxRed, MAX(count) as maxBlue FROM mytable WHERE type='blue') t
A more elegant way is conditional aggregation:
SELECT MAX(CASE WHEN t.type = 'red' THEN t.count END) as maxRed,
MAX(CASE WHEN t.type = 'blue' THEN t.count END) as maxBlue
FROM mytable t
WHERE t.type IN('blue','red')
This is how to do it:
SELECT
MAX(CASE WHEN type = 'red' THEN `count` ELSE NULL END) AS maxRed,
MAX(CASE WHEN type = 'blue' THEN `count` ELSE NULL END) AS maxBlue
FROM
mytable
WHERE type IN ('red', 'blue');
SELECT type,MAX(count) FROM mytable group by type
This will return type and maxcount in vertical and not horizontal and is scalable for n number of records.
If horizontal data doesn't matter otherwise you can use above suggested answers to use case but it will require case when then for each distinct value of data.
EDIT
Because of the comments below I tested it on real data, my version is a bit faster for a large table ,when mytable is indexed. For those who argue about it please test yourself and report back.
select
(select max(count) from mytable WHERE type='red') aa,
(select max(count) from mytable WHERE type='blue') bb

SQL select and take data as column name

I have a database which store records like:
+----+---------------------+-------------+-----------------+
| id | user_name | status| date |
+----+---------------------+-------------+-----------------+
| 1 | A | Paid| 2016-10-11|
| 2 | B | Not Paid| 2016-10-12|
| 3 | C | Paid| 2016-10-12|
| 4 | A | Not Paid| 2016-10-13|
+----+---------------------+-------------+-----------------+
I wish to obtain the results like:
+----+---------------------+-------------+-----------------+-----------------+
| id | user_name | 2016-10-11| 2016-10-12 | 2016-10-13 |
+----+---------------------+-------------+-----------------+-----------------+
| 1 | A | Paid| NA| Not Paid|
| 2 | B | NA| Not Paid| NA|
| 3 | C | NA| Paid| Na|
+----+---------------------+-------------+-----------------+-----------------+
How can I query it to obtain the results like this?
PS: Poor English
FYI: I'm using mySQL as as DBMS and here is the create script:
CREATE TABLE `moneygame`.`pay_table`(
`id` INT(11) NOT NULL AUTO_INCREMENT,
`user_name` VARCHAR(50),
`status` VARCHAR(50),
`p_date` DATE,
PRIMARY KEY (`id`)
);
If don't have a fixed number of dates then I wouldn't recommend doing what you are trying to do. Anyways, here's the solution to the problem.
create table p as select * from
(select 1 id, 'A' user_name, 'Paid' status, '2016-10-11' date union
select 2 id, 'B' user_name, 'Not Paid' status, '2016-10-12' date union
select 3 id, 'C' user_name, 'Paid' status, '2016-10-12' date union
select 4 id, 'A' user_name, 'Not Paid' status, '2016-10-13' date) t;
When dates are represented as columns
select user_name, ifnull(max(a),'NA') `2016-10-11`,
ifnull(max(b),'NA') `2016-10-12`, ifnull(max(c),'NA') `2016-10-13`
from (select user_name,
case when date = '2016-10-11' then status else null end a,
case when date = '2016-10-12' then status else null end b,
case when date = '2016-10-13' then status else null end c
from p group by user_name, date) s group by user_name;
When user names are represented as columns
Doing it this way should be optimal if you have a fixed number of users and an moving range of dates.
select date, ifnull(max(a),'NA') A,
ifnull(max(b),'NA') B, ifnull(max(c),'NA') C from
(select date,
case when user_name = 'A' then status else null end a,
case when user_name = 'B' then status else null end b,
case when user_name = 'C' then status else null end c
from p group by date, user_name) x group by date;
By the way if you still need to do dynamic columns, you should read this:
How to automate pivot tables in MySQL
MySQL pivot into dynamic number of columns
You can query like this
select user_name, max([2016-10-11]) as [2016-10-11], max([2016-10-12]) [2016-10-12],max([2016-10-13]) [2016-10-13] from #yourtable
pivot
(max(status) for date in ([2016-10-11],[2016-10-12],[2016-10-13])) p
group by user_name

SQL: select column from same table

how to select from database to show this new table into page. I'm just select from the same table. Can anyone help this?
Original Table from db
Id | Username | UserType | SpecialId
-------------------------------------
1 | jdoe | Type_A | SP_1
2 | dmatt | Type_A | SP_2
3 | kwill | Type_A | SP_3
4 | kwill_a | Type_B | SP_3
5 | dmatt_a | Type_B | SP_2
6 | dmatt_b | Type_B | SP_2
New table
No | Type_A Username | Type_B Username
-------------------------------------
1 | jdoe | -
2 | dmatt | dmatt_a, dmatt_b
3 | kwill | kwill_a
I think you may want to do this(Looks like a table pivot issue):
select
replace(SpecialId, 'SP_', '') as No,
max(case when UserType = 'Type_A' then UserName else '-' end) as Type_A_UserName,
max(case when UserType = 'Type_B' then UserName else '-' end) as Type_B_UserName
from (
select `UserType`, `SpecialId`, group_concat(UserName) as UserName
from yourtable
group by `UserType`, `SpecialId`) t
group by SpecialId
SqlFiddle Demo
Is this what you are looking for?
SELECT a.id, a.username, GROUP_CONCAT(b.username)
FROM (SELECT * FROM mytable WHERE usertype='Type_A') AS a
LEFT JOIN (SELECT * FROM mytable WHERE usertype='Type_B') AS b
ON a.specialid = b.specialid
This achieves the objective by combining a sub query with a self join.
SELECT
ifnull(tabA.UsernameA, '-') AS UsernameA,
ifnull(tabB.UsernameB, '-') AS UsernameB
FROM (
SELECT SpecialId, group_concat(Username) AS UsernameA
FROM tab
WHERE UserType = 'Type_A'
GROUP BY 1
) AS tabA
OUTER JOIN (
SELECT SpecialId, group_concat(Username) AS UsernameB
FROM tab
WHERE UserType = 'Type_B'
GROUP BY 1
) AS tabB ON tabA.SpecialId = tabB.SpecialId