MySQL display rows where value IS NULL or IN(2, 3) - mysql

I am trying to get enquiries that their teams are in 15, 9, 25, 26, 23, 18, 12 and null. The problems is the query doesn't retrieves the NULL records.
I have tried to use OR team IS NULL the query gives me all other statuses.
SELECT e.*,
FROM enquiries e
WHERE e.timestamp BETWEEN '1293840000' AND '1469055599'
AND e.status IN(1) AND e.team IN(15, 9, 25, 26, 23, 18, 12, NULL)
ORDER BY e.timestamp ASC
Any help please

Place OR condition in brackets
SELECT e.*,
FROM enquiries e
WHERE e.timestamp BETWEEN '1293840000' AND '1469055599'
AND e.status IN(1) AND ( e.team IN (15, 9, 25, 26, 23, 18, 12) OR e.team IS NULL)
ORDER BY e.timestamp ASC

Related

How to use MYSQL GROUP BY SUM with multiple columns

Basically the question is how to get the sum from #hours, groupping by #id, #code, #semester, #year columns from this
id, hours, code, semester, year
165, 6, 3, 1, 2012
166, 6, 16, 1, 2012
460, 8, 14, 2, 2012
461, 8, 16, 2, 2012
462, 15, 16, 2, 2013
463, 6, 1, 2, 2013
id, hours, code, semester, year, sum
165, 6, 3, 1, 2012, **6**
166, 6, 16, 1, 2012, **6**
460, 8, 14, 2, 2012, **8**
461, 8, 16, 2, 2012, **23**
462, 15, 16, 2, 2012, **23**
463, 6, 1, 2, 2013, **6**
I have tried a lot with GROUP BY, GROUP BY CONCAT, etc, but no success.
I want to group by all these columns in each row
Try this:
select id, code, semester, year, sum(hours) from `table` group by id,code,semester, year
to sum a values of a column you can use SUM() function of MYSQL and you can return it as on of fields like this for example the field of sum in this query would be total_hours
SELECT id,hours,code,semester,year, SUM(hours) AS total_hours
FROM table_name
GROUP BY id,code,semester, year

Return all rows from sub-query SQL?

Now I wrote query SQL for getting rows limited by first query:
SELECT * FROM commenttoarticle a
WHERE a.idCommentToArticle = (SELECT CommentToArticlePID FROM commenttoarticle b)
ORDER BY a.idCommentToArticle DESC LIMIT 3
When I try to execute this query, I get:
#1242 - Subquery returns more than 1 row
How to resolve this issue? So, I need get all rows from sub-query.
If I want return one row - I need use GROUP BY, but it is not solution
Modified query:
SELECT a.idCommentToArticle FROM
commenttoarticle a WHERE a.CommentToArticlePID IN
(SELECT idCommentToArticle FROM commenttoarticle b) ORDER BY a.idCommentToArticle DESC LIMIT 3
Dump table commenttoarticle:
CREATE TABLE IF NOT EXISTS `commenttoarticle` (
`idCommentToArticle` int(11) NOT NULL AUTO_INCREMENT,
`CommentToArticleTime` int(11) NOT NULL,
`CommentToArticleIdArticle` int(11) NOT NULL,
`CommentToArticleComment` text NOT NULL,
`CommentToArticleIdUser` int(11) NOT NULL,
`CommentToArticlePID` int(11) NOT NULL,
PRIMARY KEY (`idCommentToArticle`),
UNIQUE KEY `idCommentToArticle_UNIQUE` (`idCommentToArticle`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=59 ;
--
-- Дамп данных таблицы `commenttoarticle`
--
INSERT INTO `commenttoarticle` (`idCommentToArticle`, `CommentToArticleTime`, `CommentToArticleIdArticle`, `CommentToArticleComment`, `CommentToArticleIdUser`, `CommentToArticlePID`) VALUES
(29, 0, 11, 'продажам?\nИнтересует не мега-звезда, а именно предметный, руками умеющий продавать сам и помогающий выстраивать это бизнесам.', 459, 0),
(30, 0, 11, '2', 459, 0),
(31, 0, 11, '3', 459, 0),
(36, 0, 11, '3.1', 459, 31),
(37, 1413822798, 11, 'also facing that prob. on the plteform of win 7', 459, 29),
(38, 0, 11, ' here i dont have internet connection.. #Samint Sinha thanks ill check it out maybe tomorrow.', 459, 29),
(39, 0, 11, ' Select max id and you will have dhe last row returned', 459, 29),
(32, 0, 11, '4', 459, 0),
(44, 1414354324, 11, 'How to do', 456, 29),
(45, 1414354469, 11, 'sfsfsf', 456, 29),
(46, 1414354708, 11, 'dddd', 456, 29),
(47, 1414357761, 11, 'sfsfs', 456, 0),
(57, 1414370833, 39, 'kkkppppppp', 456, 0),
(49, 1414358233, 11, 'VSF\nSFSF', 456, 0),
(50, 1414359589, 11, 'How to do', 456, 0),
(51, 1414359660, 11, 'sfsfsdf', 456, 0),
(52, 1414361057, 11, 'SDFSF', 456, 0),
(53, 1414364023, 11, 'dsfdsjfsifmsi', 456, 0),
(54, 1414364031, 11, 'sdfdskjfnskf', 456, 52),
(55, 1414364034, 11, 'sdfdskjfnskf', 456, 52),
(56, 1414364044, 11, 'fndsdfnsofosfi', 456, 52),
(58, 1414370841, 39, 'dfgdfgdgdgdgdgdfgdgdfg', 456, 0);
Result what I need:
Here is example at sqlfiddle: sqlfiddle.com/#!2/dbd82a/1 I need get last 3 rows with a unlimited COMMENTTOARTICLEPID for each first query, if exists. In example, I need get rows with IDCOMMENTTOARTICLE: 58, 57, 56, 52
SELECT a.*
FROM commenttoarticle a
JOIN commenttoarticle b
ON b.CommentToArticlePID = a.idCommentToArticle
ORDER
BY a.idCommentToArticle DESC
LIMIT 3
use IN instead of '=' before sub-select.
USe in instead of ==
SELECT * FROM commenttoarticle a WHERE a.idCommentToArticle in (SELECT CommentToArticlePID FROM commenttoarticle b) ORDER BY a.idCommentToArticle DESC LIMIT 3
Use IN instead of = because your query returns lot of values:
SELECT * FROM commenttoarticle a
WHERE a.idCommentToArticle IN (
SELECT CommentToArticlePID
FROM commenttoarticle b)
ORDER BY a.idCommentToArticle DESC LIMIT 3
You can use limit in both sub query or main query or both.
If you need only 3 result then add limit at main query
SELECT * FROM commenttoarticle a WHERE a.idCommentToArticle IN (SELECT CommentToArticlePID FROM commenttoarticle b) ORDER BY a.idCommentToArticle DESC LIMIT 3
If you want 3 result at sub query then it will be like this
SELECT * FROM commenttoarticle a WHERE a.idCommentToArticle IN (SELECT CommentToArticlePID FROM commenttoarticle b LIMIT 3) ORDER BY a.idCommentToArticle DESC
If you want both
SELECT * FROM commenttoarticle a WHERE a.idCommentToArticle IN (SELECT CommentToArticlePID FROM commenttoarticle b LIMIT 3) ORDER BY a.idCommentToArticle DESC LIMIT 3
I am thinking that your subquery returns more than 1 row hence replace your "=" with "IN". Like this...
SELECT * FROM commenttoarticle a
WHERE a.idCommentToArticle IN (SELECT CommentToArticlePID FROM commenttoarticle b)
ORDER BY a.idCommentToArticle DESC LIMIT 3

MySQL with Where IN and Distinct and Limit

SELECT id, server_id, start_time, end_time
FROM errors
WHERE server_id in (3, 12, 24, 25, 26, 27, 28, 29, 30)
ORDER BY id DESC
LIMIT 9
This is the query I'm trying to run to give me results where the server_id = 3, 12, 24, 25, 26, 27, 28, 29, 30. Instead, what I receive is server_id = 25, 25, 12, 25, 27, 27, 28, 28, 27. Note the repeating server_ids. The query gives me unique id but duplicate server_id.
Is there a way I can get results that would show me the last id for each server_id?
I've tried doing ORDER BY server_id but that gives me the same issue.
I tried running DISTINCT but that also does not work.
you'll have to use some aggregation functions.
Something like
select
server_id,
max(id),
avg(start_time),--for example
avg(end_time)--for example
from errors
where server_id in (3, 12, 24, 25, 26, 27, 28, 29, 30)
group by server_id
order by id desc
if you need tht start_time and end_time corresponding to the max id by server_id, you may do
select e.id, e.server_id, e.start_time, e.end_time
from errors e
join (select server_id, max(id) maxid
from errors
group by server_id) t
on t.maxid = e.id and e.server_id = t.server_id
where e.server_id in (3, 12, 24, 25, 26, 27, 28, 29, 30)
order by e.id DESC
The issue you have is that you need only one record from each server with the max ID.. and relative information. You need to limit the results to just that max ID... Here's one way...
SELECT id, server_id, start_time, end_time
FROM errors
WHERE server_id in (3, 12, 24, 25, 26, 27, 28, 29, 30)
and ID = (Select max(ID) from errors E2 where E2.server_ID=errors.server_ID)
ORDER BY id DESC
LIMIT 9

MySQL include zero rows when using COUNT with LEFT OUTER JOIN and GROUP BY

How can I avoid eliminating the users with zero meetings? I'm aware there are similar questions, but this code is quite a bit more complex.
SELECT user.userID, user.contactName, user.email, COUNT( * ) AS meetingsCount
FROM user
LEFT OUTER JOIN meeting ON user.userID = meeting.userID
WHERE user.userID NOT
IN ( 1, 2, 3, 4, 5, 59, 62, 63, 64, 66, 69, 71, 72, 73, 78, 107 )
AND SUBSTRING( meeting.meetingCode, 5, 2 )
BETWEEN 12
AND 22
AND SUBSTRING( meeting.meetingCode, 7, 2 )
BETWEEN 01
AND 12
AND SUBSTRING( meeting.meetingCode, 9, 2 )
BETWEEN 01
AND 31
GROUP BY user.userID, contactName, email
ORDER BY meetingsCount DESC
You need to put the logic for the meeting code table in your join. Otherwise users matching the records you are filtering out from the meeting table will be filtered out of your results. Making your JOIN essentially an INNER join. I think you also should put single quotes around the values in your BETWEEN clauses.
SELECT user.userID, user.contactName, user.email, COUNT( meeting.userID ) AS meetingsCount
FROM user
LEFT OUTER JOIN meeting ON user.userID = meeting.userID
AND SUBSTRING( meeting.meetingCode, 5, 2 ) BETWEEN '12' AND '22'
AND SUBSTRING( meeting.meetingCode, 7, 2 ) BETWEEN '01' AND '12'
AND SUBSTRING( meeting.meetingCode, 9, 2 ) BETWEEN '01' AND '31'
WHERE user.userID NOT IN ( 1, 2, 3, 4, 5, 59, 62, 63, 64, 66, 69, 71, 72, 73, 78, 107 )
GROUP BY user.userID, contactName, email
ORDER BY meetingsCount DESC

MySql query and GROUP BY, what do I need to do differently?

Right now I have an sql query that selects a range of of my sales reps, then joins a customer table, and counts if certain values are true. My query works, however, It leaves out sales reps who have not sold any customers. I would like this query to return all sales reps, even if they have not created any client records matching the criteria. What do I need to change?
Here is a link to my SQL Query: http://pastie.org/4557540 (same as below)
SELECT u.id AS
`employee_id`,
u.`first_name` AS
`employee_first_name`,
u.`last_name` AS
`employee_last_name`,
(SELECT Count(*)
FROM saleset s
WHERE s.pitchedby_id = `employee_id`
AND s.pitchstartedat BETWEEN '2012-08-20 00:00:00' AND
'2012-08-20 23:59:59') AS
`transfers_taken`,
Count(IF(c.`saletype_id` IS NOT NULL, 1, NULL)) AS
`total_closes`,
Count(IF(c.`saletype_id` = 1, 1, NULL)) AS
`regular_sale`,
Count(IF(c.`saletype_id` = 2, 1, NULL)) AS
`postdated_sale`,
Count(IF(c.`saletype_id` = 4, 1, NULL)) AS
`attempted_sale`,
Count(IF(c.`customerstatus_id` IN ( 8, 18, 23 ), 1, NULL)) AS
`cancel_status`,
Count(IF(c.`customerstatus_id` IN ( 1, 12, 13, 24 ), 1, NULL)) AS
`pending_completion_status`,
Count(IF(c.`customerstatus_id` IN ( 5, 6, 16 ), 1, NULL)) AS
`complete_status`,
Count(IF(c.`customerstatus_id` = 20, 1, NULL)) AS
`postdate_pending`,
Count(IF(c.`customerstatus_id` = 25, 1, NULL)) AS
`postdate_declined`
FROM `user` u
LEFT JOIN customer c
ON c.`salesrep_id` = u.id
WHERE u.id IN ( 39, 65, 76, 96,
195, 266, 349, 401,
402, 404, 405, 407,
411, 412 )
AND c.`activationdate` BETWEEN
'2012-08-20 00:00:00' AND '2012-08-20 23:59:59'
GROUP BY u.`id`
The problem is that your are filtering the customers in the WHERE clause, after the LEFT JOIN is specified, so salesman with no sales get left out, because c.activationdate is null for these records.
The solution is having the client filtering inside the join conditions:
(...)
FROM `user` u
LEFT JOIN customer c
ON (c.`salesrep_id` = u.id and
u.id IN ( 39, 65, 76, 96, 195, 266, 349, 401,
402, 404, 405, 407,
411, 412 )
AND c.`activationdate` BETWEEN
'2012-08-20 00:00:00' AND '2012-08-20 23:59:59')
GROUP BY u.`id`
Perhaps you could take what you have an union it with your sales reps that have no sales, and just hard code them to 0. Something like:
SELECT u.id AS
`employee_id`,
u.`first_name` AS
`employee_first_name`,
u.`last_name` AS
`employee_last_name`,
(SELECT Count(*)
FROM saleset s
WHERE s.pitchedby_id = `employee_id`
AND s.pitchstartedat BETWEEN '2012-08-20 00:00:00' AND
'2012-08-20 23:59:59') AS
`transfers_taken`,
Count(IF(c.`saletype_id` IS NOT NULL, 1, NULL)) AS
`total_closes`,
Count(IF(c.`saletype_id` = 1, 1, NULL)) AS
`regular_sale`,
Count(IF(c.`saletype_id` = 2, 1, NULL)) AS
`postdated_sale`,
Count(IF(c.`saletype_id` = 4, 1, NULL)) AS
`attempted_sale`,
Count(IF(c.`customerstatus_id` IN ( 8, 18, 23 ), 1, NULL)) AS
`cancel_status`,
Count(IF(c.`customerstatus_id` IN ( 1, 12, 13, 24 ), 1, NULL)) AS
`pending_completion_status`,
Count(IF(c.`customerstatus_id` IN ( 5, 6, 16 ), 1, NULL)) AS
`complete_status`,
Count(IF(c.`customerstatus_id` = 20, 1, NULL)) AS
`postdate_pending`,
Count(IF(c.`customerstatus_id` = 25, 1, NULL)) AS
`postdate_declined`
FROM `user` u
INNER JOIN customer c
ON c.`salesrep_id` = u.id
WHERE u.id IN ( 39, 65, 76, 96,
195, 266, 349, 401,
402, 404, 405, 407,
411, 412 )
AND c.`activationdate` BETWEEN
'2012-08-20 00:00:00' AND '2012-08-20 23:59:59'
GROUP BY u.`id`
UNION
SELECT u.id AS
`employee_id`,
u.`first_name` AS
`employee_first_name`,
u.`last_name` AS
`employee_last_name`,
0 AS
`transfers_taken`,
Count(IF(c.`saletype_id` IS NOT NULL, 1, NULL)) AS
`total_closes`,
Count(IF(c.`saletype_id` = 1, 1, NULL)) AS
`regular_sale`,
Count(IF(c.`saletype_id` = 2, 1, NULL)) AS
`postdated_sale`,
Count(IF(c.`saletype_id` = 4, 1, NULL)) AS
`attempted_sale`,
Count(IF(c.`customerstatus_id` IN ( 8, 18, 23 ), 1, NULL)) AS
`cancel_status`,
Count(IF(c.`customerstatus_id` IN ( 1, 12, 13, 24 ), 1, NULL)) AS
`pending_completion_status`,
Count(IF(c.`customerstatus_id` IN ( 5, 6, 16 ), 1, NULL)) AS
`complete_status`,
Count(IF(c.`customerstatus_id` = 20, 1, NULL)) AS
`postdate_pending`,
Count(IF(c.`customerstatus_id` = 25, 1, NULL)) AS
`postdate_declined`
FROM `user` u
LEFT JOIN customer c
ON c.`salesrep_id` = u.id
WHERE u.id IN ( 39, 65, 76, 96,
195, 266, 349, 401,
402, 404, 405, 407,
411, 412 )
AND c.`activationdate` BETWEEN
'2012-08-20 00:00:00' AND '2012-08-20 23:59:59'
AND c.`salesrep_id` IS NULL
GROUP BY u.`id`