Mysql Between function not working in join - mysql

I have a query where is first check the LIKE in the productpost table with the value where the propertyid is 1 or 4 then,(upto this it work perfectly) AND but the next part where propertyid is 3 and has value 0 to 50 (this part of query brings all the results in database the result, whereas it should have brought only the ones that has value 0 to 50 in propertyid 3
productpost table:
I have tried with many things instead of between but it does not work, everything is ok before the last part of the code
SELECT COUNT(DISTINCT a.id) as `num` FROM product a INNER JOIN customer b ON a.userid=b.cid INNER JOIN productcategory c ON a.id=c.id INNER JOIN productpost d ON a.adid=d.adid WHERE a.verif=1 AND c.cattype=1 AND (d.value LIKE '%a%') AND (d.propertyid in (1,4)) AND (d.propertyid=3 AND d.value BETWEEN 0 AND 50)
Everything works perfectly but this part, but the addition of this part starts to give wrong results
(d.propertyid=3 AND d.value BETWEEN 0 AND 50)
EXAMPLE :
If I give an example it would be that if there are 4 "adid" and 3 of them has LIKE '%a%' in value in propertyid 1 and 4, and among those three, 2 of them has value 0 to 50 in propertid 3, so in first condition it filter out three and in the next condition it filters value between 0 to 50 and at the end shows two "adid".
THE MCRE CODE THAT I WANT BUT IS NOT WORKING :
SELECT DISTINCT(adid) FROM productpost WHERE (value LIKE '%a%' AND (propertyid in (1,4)) AND (SELECT * FROM productpost WHERE propertyid=3 AND value BETWEEN 501 AND 510)
CREATE TABLE `adcategory` (
`id` int(99) NOT NULL,
`adid` varchar(99) NOT NULL,
`cattype` varchar(99) NOT NULL,
`catvalue` varchar(99) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `adcategory` (`id`, `adid`, `cattype`, `catvalue`) VALUES
(15, 'a98e7e97e2', '1', '1'),
(16, 'a98e7e97e2', '2', '2'),
(17, 'ad129031b6', '1', '1'),
(18, 'ad129031b6', '2', '2'),
(19, '0906c4d03d', '1', '1'),
(20, '0906c4d03d', '2', '2'),
(21, '09120c53c7', '1', '1'),
(22, '09120c53c7', '2', '2'),
(23, '09b8369032', '1', '2'),
(24, '09b8369032', '2', '1'),
(25, '7044c7a086', '1', '2'),
(26, '7044c7a086', '2', '1'),
(27, '7065f93e14', '1', '1'),
(28, '7065f93e14', '2', '2'),
(29, '70e57235fd', '1', '2'),
(30, '70e57235fd', '2', '1'),
(31, 'fd7a623a9d', '1', '1'),
(32, 'fd7a623a9d', '2', '2'),
(33, 'fd7a623a9d', '3', '2'),
(34, 'fd81353461', '1', '1'),
(35, 'fd81353461', '2', '2'),
(36, 'fd81353461', '3', '2'),
(37, 'fd8762b7c2', '1', '1'),
(38, 'fd8762b7c2', '2', '2'),
(39, 'fd8762b7c2', '3', '2'),
(40, 'fd8f011e8a', '1', '1'),
(41, 'fd8f011e8a', '2', '2'),
(42, 'fd8f011e8a', '3', '2'),
(43, 'fd9d60b084', '1', '1'),
(44, 'fd9d60b084', '2', '2'),
(45, 'fd9d60b084', '3', '2'),
(46, 'fda24d8327', '1', '1'),
(47, 'fda24d8327', '2', '2'),
(48, 'fda24d8327', '3', '2'),
(49, 'fda8203702', '1', '1'),
(50, 'fda8203702', '2', '2'),
(51, 'fda8203702', '3', '2'),
(52, 'fda8ca38e1', '1', '1'),
(53, 'fda8ca38e1', '2', '2'),
(54, 'fda8ca38e1', '3', '2'),
(55, 'fda97268c1', '1', '2'),
(56, 'fda97268c1', '2', '2'),
(57, 'fda97268c1', '3', '2'),
(58, 'fdb5f4f569', '1', '2'),
(59, 'fdb5f4f569', '2', '2'),
(60, 'fdb5f4f569', '3', '2'),
(61, 'fdbc31a522', '1', '2'),
(62, 'fdbc31a522', '2', '2'),
(63, 'fdbc31a522', '3', '2'),
(64, 'fdc85be575', '1', '2'),
(65, 'fdc85be575', '2', '2'),
(66, 'fdc85be575', '3', '2'),
(67, '0b4da6e344', '1', '1'),
(68, '0b4da6e344', '2', '2'),
(69, '0b4da6e344', '3', '2'),
(70, '0bb36c6c89', '1', '2'),
(71, '0bb36c6c89', '2', '1'),
(72, '0c00cd00d3', '1', '1'),
(73, '0c00cd00d3', '2', '2'),
(74, '0c00cd00d3', '3', '2'),
(75, '0c046278c6', '1', '1'),
(76, '0c046278c6', '2', '2'),
(77, '0c046278c6', '3', '2'),
(78, '0c0e4bf06b', '1', '2'),
(79, '0c0e4bf06b', '2', '2'),
(80, '0c0e4bf06b', '3', '2'),
(81, '0c140b726b', '1', '2'),
(82, '0c140b726b', '2', '2'),
(83, '0c140b726b', '3', '2'),
(84, '0f6e44804f', '1', '2'),
(85, '0f6e44804f', '2', '2'),
(86, '0f6e44804f', '3', '2'),
(87, '0f857cce9a', '1', '1'),
(88, '0f857cce9a', '2', '2'),
(89, '0f857cce9a', '3', '2'),
(90, '0ff41014f1', '1', '1'),
(91, '0ff41014f1', '2', '2'),
(92, '0ff41014f1', '3', '2'),
(93, '101f76e659', '1', '2'),
(94, '101f76e659', '2', '2'),
(95, '101f76e659', '3', '2'),
(96, '1026497b37', '1', '1'),
(97, '1026497b37', '2', '2'),
(98, '1026497b37', '3', '2'),
(99, '15df6d4a01', '1', '2'),
(100, '15df6d4a01', '2', '1'),
(101, '15e2329fa0', '1', '1'),
(102, '15e2329fa0', '2', '2'),
(103, '15e2329fa0', '3', '2'),
(104, '15e4e85639', '1', '1'),
(105, '15e4e85639', '2', '2'),
(106, '15e4e85639', '3', '2');
CREATE TABLE `adpost` (
`id` int(99) NOT NULL,
`adid` varchar(99) NOT NULL,
`propertyid` varchar(99) NOT NULL,
`value` varchar(99) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `adpost` (`id`, `adid`, `propertyid`, `value`) VALUES
(49, 'a98e7e97e2', '1', 'ad 1'),
(50, 'a98e7e97e2', '2', '0'),
(51, 'a98e7e97e2', '3', '120'),
(52, 'a98e7e97e2', '4', 'z'),
(53, 'ad129031b6', '1', 'teasss'),
(54, 'ad129031b6', '2', '1'),
(55, 'ad129031b6', '3', '120'),
(56, 'ad129031b6', '4', 'sdfr'),
(57, '0906c4d03d', '1', 'test 11111111'),
(58, '0906c4d03d', '2', '1'),
(59, '0906c4d03d', '3', '21'),
(60, '0906c4d03d', '4', 'asasassassa'),
(61, '09120c53c7', '1', 'MySql PDO not passing value from variable in php function'),
(62, '09120c53c7', '2', '1'),
(63, '09120c53c7', '3', '130'),
(64, '09120c53c7', '4', 'sdfghjklkmnbvftyhj'),
(65, '0955d0d4d2', '1', 'MySql PDO not passing value from variable in php function'),
(66, '0955d0d4d2', '2', '1'),
(67, '0955d0d4d2', '3', '130'),
(68, '0955d0d4d2', '4', 'fygvbhnj bkjhljkn,blhuijkn'),
(69, '09b8369032', '1', 'MySql PDO not passing value from variable in php function'),
(70, '09b8369032', '2', '0'),
(71, '09b8369032', '3', '133'),
(72, '09b8369032', '4', 'k;nkldfnsdkfndlkfndsnldsn'),
(73, '7044c7a086', '1', 'isset'),
(74, '7044c7a086', '2', '1'),
(75, '7044c7a086', '3', '123'),
(76, '7044c7a086', '4', 'qwdeqw'),
(77, '7065f93e14', '1', 'why do we use php()?'),
(78, '7065f93e14', '2', '1'),
(79, '7065f93e14', '3', '123'),
(80, '7065f93e14', '5', 'on'),
(81, '7065f93e14', '4', 'qweqweqw'),
(82, '70e57235fd', '1', 'why do we use bindparam()?'),
(83, '70e57235fd', '2', '1'),
(84, '70e57235fd', '3', '111'),
(85, '70e57235fd', '5', 'on'),
(86, '70e57235fd', '4', 'assasa'),
(87, 'fd7a623a9d', '1', 'IMAGE RESIXE'),
(88, 'fd7a623a9d', '2', '1'),
(89, 'fd7a623a9d', '3', 'NASN'),
(90, 'fd7a623a9d', '5', 'on'),
(91, 'fd7a623a9d', '4', 'SABIK'),
(92, 'fd81353461', '1', '11'),
(93, 'fd81353461', '2', '1'),
(94, 'fd81353461', '3', '1'),
(95, 'fd81353461', '5', 'on'),
(96, 'fd81353461', '4', '1111'),
(97, 'fd8762b7c2', '1', '11'),
(98, 'fd8762b7c2', '2', '1'),
(99, 'fd8762b7c2', '3', '1'),
(100, 'fd8762b7c2', '5', 'on'),
(101, 'fd8762b7c2', '4', '1111'),
(102, 'fd8f011e8a', '1', 'qq'),
(103, 'fd8f011e8a', '2', '0'),
(104, 'fd8f011e8a', '3', '11'),
(105, 'fd8f011e8a', '4', 'www'),
(106, 'fd9d60b084', '1', 'why do we use bindparam()?'),
(107, 'fd9d60b084', '2', '0'),
(108, 'fd9d60b084', '3', 'aaa'),
(109, 'fd9d60b084', '4', 's'),
(110, 'fda24d8327', '1', 'why do we use bindparam()?'),
(111, 'fda24d8327', '2', '0'),
(112, 'fda24d8327', '3', 'aaa'),
(113, 'fda24d8327', '4', 's'),
(114, 'fda8203702', '1', 'why do we use bindparam()?'),
(115, 'fda8203702', '2', '0'),
(116, 'fda8203702', '3', 'aaa'),
(117, 'fda8203702', '4', 's'),
(118, 'fda8ca38e1', '1', 'why do we use bindparam()?'),
(119, 'fda8ca38e1', '2', '0'),
(120, 'fda8ca38e1', '3', 'aaa'),
(121, 'fda8ca38e1', '4', 's'),
(122, 'fda97268c1', '1', ''),
(123, 'fda97268c1', '2', '0'),
(124, 'fda97268c1', '3', ''),
(125, 'fda97268c1', '4', ''),
(126, 'fdb5f4f569', '1', ''),
(127, 'fdb5f4f569', '2', '0'),
(128, 'fdb5f4f569', '3', ''),
(129, 'fdb5f4f569', '4', ''),
(130, 'fdbc31a522', '1', ''),
(131, 'fdbc31a522', '2', '0'),
(132, 'fdbc31a522', '3', ''),
(133, 'fdbc31a522', '4', ''),
(134, 'fdc85be575', '1', ''),
(135, 'fdc85be575', '2', '0'),
(136, 'fdc85be575', '3', 'aa'),
(137, 'fdc85be575', '4', ''),
(138, '0b4da6e344', '1', 'test'),
(139, '0b4da6e344', '2', '1'),
(140, '0b4da6e344', '3', '123'),
(141, '0b4da6e344', '5', 'on'),
(142, '0b4da6e344', '4', '12'),
(143, '0bb36c6c89', '1', 'MySql PDO not passing value from variable in php function'),
(144, '0bb36c6c89', '2', '1'),
(145, '0bb36c6c89', '3', '123'),
(146, '0bb36c6c89', '5', 'on'),
(147, '0bb36c6c89', '4', '123'),
(148, '0c00cd00d3', '1', 'aaa'),
(149, '0c00cd00d3', '2', '0'),
(150, '0c00cd00d3', '3', 'as'),
(151, '0c00cd00d3', '4', 'a'),
(152, '0c046278c6', '1', 'aaa'),
(153, '0c046278c6', '2', '0'),
(154, '0c046278c6', '3', 'a'),
(155, '0c046278c6', '4', 'a'),
(156, '0c0e4bf06b', '1', 'a'),
(157, '0c0e4bf06b', '2', '0'),
(158, '0c0e4bf06b', '3', 'a'),
(159, '0c0e4bf06b', '4', ''),
(160, '0c140b726b', '1', 'as'),
(161, '0c140b726b', '2', '0'),
(162, '0c140b726b', '3', '510'),
(163, '0c140b726b', '4', ''),
(164, '0f6e44804f', '1', 'a'),
(165, '0f6e44804f', '2', '1'),
(166, '0f6e44804f', '3', '1'),
(167, '0f6e44804f', '4', ''),
(168, '0f857cce9a', '1', 'q'),
(169, '0f857cce9a', '2', '0'),
(170, '0f857cce9a', '3', '123'),
(171, '0f857cce9a', '4', ''),
(172, '0ff41014f1', '1', '1'),
(173, '0ff41014f1', '2', '0'),
(174, '0ff41014f1', '3', '1'),
(175, '0ff41014f1', '4', ''),
(176, '101f76e659', '1', 'asdfg'),
(177, '101f76e659', '2', '0'),
(178, '101f76e659', '3', '1'),
(179, '101f76e659', '4', ''),
(180, '1026497b37', '1', '123'),
(181, '1026497b37', '2', '0'),
(182, '1026497b37', '3', '123'),
(183, '1026497b37', '4', ''),
(184, '15df6d4a01', '1', 'qwe'),
(185, '15df6d4a01', '2', '0'),
(186, '15df6d4a01', '3', '501'),
(187, '15df6d4a01', '4', ''),
(188, '15e2329fa0', '1', '123'),
(189, '15e2329fa0', '2', '0'),
(190, '15e2329fa0', '3', '123'),
(191, '15e2329fa0', '4', ''),
(192, '15e4e85639', '1', '123'),
(193, '15e4e85639', '2', '0'),
(194, '15e4e85639', '3', '123'),
(195, '15e4e85639', '4', '');
CREATE TABLE `adpostuser` (
`id` int(99) NOT NULL,
`adid` varchar(99) NOT NULL,
`userid` varchar(99) NOT NULL,
`posttime` datetime NOT NULL DEFAULT current_timestamp(),
`verif` varchar(9) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `adpostuser` (`id`, `adid`, `userid`, `posttime`, `verif`) VALUES
(1, 'a98e7e97e2', '1', '2019-08-31 21:57:27', '1'),
(2, 'ad129031b6', '1', '2019-09-01 01:57:29', '1'),
(3, '09b8369032', '1', '2019-09-17 14:38:27', '1'),
(4, '0906c4d03d', '2', '2019-09-18 00:44:53', '1'),
(5, '09120c53c7', '2', '2019-09-18 00:44:53', '1'),
(6, '7044c7a086', '1', '2019-10-04 14:35:24', '2'),
(7, '7065f93e14', '1', '2019-10-04 14:44:15', '2'),
(8, '70e57235fd', '1', '2019-10-04 15:18:15', '1'),
(9, 'fd7a623a9d', '1', '2019-11-04 13:47:50', '1'),
(10, 'fd81353461', '1', '2019-11-04 13:49:39', '1'),
(11, 'fd8762b7c2', '1', '2019-11-04 13:51:18', '1'),
(12, 'fd8f011e8a', '1', '2019-11-04 13:53:20', '1'),
(13, 'fd9d60b084', '1', '2019-11-04 13:57:10', '1'),
(14, 'fda1d5f5fd', '1', '2019-11-04 13:58:21', '1'),
(15, 'fda24d8327', '1', '2019-11-04 13:58:28', '1'),
(16, 'fda8203702', '1', '2019-11-04 14:00:02', '1'),
(17, 'fda8ca38e1', '1', '2019-11-04 14:00:12', '1'),
(18, 'fda97268c1', '1', '2019-11-04 14:00:23', '1'),
(19, 'fdb5f4f569', '1', '2019-11-04 14:03:43', '1'),
(20, 'fdbc31a522', '1', '2019-11-04 14:05:23', '1'),
(21, 'fdc85be575', '1', '2019-11-04 14:08:37', '1'),
(22, '0b4da6e344', '1', '2019-11-05 05:31:38', '1'),
(23, '0bb36c6c89', '1', '2019-11-05 05:58:46', '0'),
(24, '0c00cd00d3', '1', '2019-11-05 06:19:24', '0'),
(25, '0c046278c6', '1', '2019-11-05 06:20:22', '0'),
(26, '0c0e4bf06b', '1', '2019-11-05 06:23:00', '0'),
(27, '0c140b726b', '1', '2019-11-05 06:24:32', '0'),
(28, '0f6e44804f', '1', '2019-11-05 10:13:24', '0'),
(29, '0f857cce9a', '1', '2019-11-05 10:19:35', '0'),
(30, '0ff41014f1', '1', '2019-11-05 10:49:05', '0'),
(31, '101f76e659', '1', '2019-11-05 11:00:39', '0'),
(32, '1026497b37', '1', '2019-11-05 11:02:28', '0'),
(33, '15df6d4a01', '1', '2019-11-05 17:33:10', '0'),
(34, '15e2329fa0', '1', '2019-11-05 17:33:55', '0'),
(35, '15e4e85639', '1', '2019-11-05 17:34:38', '0');
CREATE TABLE `adproperty` (
`id` int(99) NOT NULL,
`property` varchar(99) NOT NULL,
`property2` varchar(99) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `adproperty` (`id`, `property`, `property2`) VALUES
(1, 'Title', 'title'),
(2, 'Type of Ad', 'typeofad'),
(3, 'Price', 'price'),
(4, 'Description', 'description'),
(5, 'Negotiable', 'negotiable');
CREATE TABLE `student` (
`stid` int(11) NOT NULL,
`fname` varchar(99) NOT NULL,
`lname` varchar(99) NOT NULL,
`gender` varchar(11) NOT NULL,
`email` varchar(99) NOT NULL,
`mob` varchar(99) NOT NULL,
`address` varchar(999) NOT NULL,
`uni` varchar(99) NOT NULL,
`uniid` varchar(99) NOT NULL,
`password` varchar(999) NOT NULL,
`city` varchar(99) NOT NULL,
`fb` varchar(99) NOT NULL,
`twitter` varchar(99) NOT NULL,
`linkedin` varchar(999) NOT NULL,
`about` varchar(99) NOT NULL,
`pro_pic` varchar(99) NOT NULL DEFAULT 'default.png',
`tespass` varchar(99) NOT NULL,
`code` varchar(99) NOT NULL,
`membersince` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `student` (`stid`, `fname`, `lname`, `gender`, `email`, `mob`, `address`, `uni`, `uniid`, `password`, `city`, `fb`, `twitter`, `linkedin`, `about`, `pro_pic`, `tespass`, `code`, `membersince`) VALUES
(1, 'aa111111ssssssss', '222ss', '', '1#h.com', '18226239935', 'cox bazafrhhh', '2', '1', '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92', '', '', '', 'linkedin.com/1.h', '', 'default.png', '1', 'ahWHvPmIJnpw', '2019-09-15 03:09:51'),
(2, 'sabik', 'sabik', '', 'sab#gmai.com', '65789', '6789', '1', '', '6e7d04d2b700028a8f47464737a6257f9652f277e26ebe6c532f01d40f6ac8c8', '', '', '', '', '', '', '0', '', '2019-09-15 03:09:51'),
(3, 'sabikasdasd', 'qwerty', '', 'w#w.comaaza', '1', 'q2w23', '1', 'qwe', '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92', 'sss', 'aaa', 'lk', '', 'sssaaqaaaaqw', 'default.png', '0', '', '2019-09-15 03:09:51'),
(4, 'sabik', 'sabik', '', 'b#gmail.com', '3456789', '567890', '1', '89', 'cd70bea023f752a0564abb6ed08d42c1440f2e33e29914e55e0be1595e24f45a', '', '', '', '', '', '', '0', '', '2019-09-15 03:09:51'),
(5, 'sabik', 'sabik', '', 'b2#gmail.com', '3456789', '567890', '1', '89', '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92', '', '', '', '', '', '', '0', '', '2019-09-15 03:09:51'),
(6, 'sbaik', 'sanioasndoas', '', 'nasnd#gmail.com', 'knasldn', 'lkjsndflkndsa', '1', '123123123', '66e2beec7e35b80fc9a2c46d41caa10c63632ed1d76e31e731fc9a34cee7c566', '', '', '', '', '', '', '0', '', '2019-09-15 03:09:51'),
(7, 'Taeef', 'Najib', '', 'taeefnajib#gmail.com', '001937', 'defghjkl;', '2', '123456789', '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92', '', '', '', '', '', '', '1', 'ahWHvPmIJnpw', '2019-09-15 03:09:51'),
(8, 'asif', 'imtiaz', 'm', 'asif#ghoorni.com', '01685726653', '', '1', '15126626', 'f0eee373417d6cee2d80f67483132ab44369c0ff115015a39745feba9dc736ac', '', '', '', '', '', '', '0', 'zTqbNEgmZCa7', '2019-11-05 05:16:30');
CREATE TABLE `students` (
`stid` int(11) NOT NULL,
`fname` varchar(99) NOT NULL,
`lname` varchar(99) NOT NULL,
`email` varchar(99) NOT NULL,
`mob` varchar(99) NOT NULL,
`address` varchar(999) NOT NULL,
`uni` varchar(99) NOT NULL,
`uniid` varchar(99) NOT NULL,
`password` varchar(999) NOT NULL,
`verif` varchar(99) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `uni` (
`id` int(99) NOT NULL,
`uniname` varchar(999) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `uni` (`id`, `uniname`) VALUES
(1, 'Ahsanullah University of Science and Technology'),
(2, 'NSU');
FIDDLE LINK :
https://www.db-fiddle.com/f/5YKoERT5fwZ5dwCoVQPWbi/0#&togetherjs=Jaq8cRqBti

Try using OR, I didn't realized that this will work in mysql, if you try to run this query in postgres or mssql, it will fail.
SELECT COUNT(DISTINCT a.id) as `num`
FROM product a
INNER JOIN customer b ON a.userid=b.cid
INNER JOIN productcategory c ON a.id=c.id
INNER JOIN productpost d ON a.adid=d.adid
WHERE a.verif=1 AND c.cattype=1 AND (d.value LIKE '%a%')
AND (d.propertyid in (1,4) OR (d.propertyid=3 AND d.value BETWEEN 0 AND 50))
see dbfiddle

Consider the following:
SELECT '23' BETWEEN '220' AND '240';
+------------------------------+
| '23' BETWEEN '220' AND '240' |
+------------------------------+
| 1 |
+------------------------------+
Can you see what's wrong here?

Related

Absenteeism query showing date as employee is absent

I'm trying to generate an absenteeism query with the date employees were absent.
Table "ci_admin":
DROP TABLE IF EXISTS `ci_admin`;
CREATE TABLE `ci_admin` (
`admin_id` int(11) NOT NULL AUTO_INCREMENT,
`admin_role_id` int(11) NOT NULL,
`department_id` int(11) NOT NULL,
`firstname` varchar(255) NOT NULL,
`lastname` varchar(255) NOT NULL,
`emp_photo` varchar(255) NOT NULL,
PRIMARY KEY (`admin_id`)
);
INSERT INTO `ci_admin` VALUES ('1', '20170079320', '1', '1', 'AHMED', 'MAKOKHA', '125.jpg'), ('2', '20170079321', '1', '1', 'MARIAM', 'AHMED', '2.png'), ('3', '20170080091', '2', '3', 'SALAHUDEEN', 'AHMED', '3.png'), ('4', '20130080083', '2', '2', 'NURU', 'ABBAS', '4.png'), ('5', '20170080092', '5', '1', 'HIRBO', 'ALI', '5.png');
Table "ci_department":
DROP TABLE IF EXISTS `ci_department`;
CREATE TABLE `ci_department` (
`department_id` int(11) NOT NULL AUTO_INCREMENT,
`admin_id` int(11) NOT NULL,
`department_name` varchar(225) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`department_id`)
);
INSERT INTO `ci_department` VALUES ('1', 'INFORMATION COMMUNICATION TECHNOLOGY'), ('2', 'HUMAN RESOURCE'), ('3', 'PROCUREMENT');
Table "ci_admnin_roles":
DROP TABLE IF EXISTS `ci_admin_roles`;
CREATE TABLE `ci_admin_roles` (
`admin_role_id` int(11) NOT NULL AUTO_INCREMENT,
`admin_role_title` varchar(30) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`admin_role_id`)
);
INSERT INTO `ci_admin_roles` VALUES ('1', 'Super Admin'), ('2', 'ADMIN'), ('3', 'ACCOUNTANT'), ('4', 'OPERATOR'), ('5', 'IT OFFICER'), ('6', 'APPLICANT');
Table "ci_attendance":
DROP TABLE IF EXISTS `ci_attendance`;
CREATE TABLE `ci_attendance` (
`attendance_id` int(11) NOT NULL AUTO_INCREMENT,
`attendance_date` date NOT NULL,
`admin_id` int(11) NOT NULL,
`check_in` time NOT NULL,
`check_out` time NOT NULL,
PRIMARY KEY (`attendance_id`)
);
INSERT INTO `ci_attendance` VALUES ('1', '2022-12-22', '1', '08:15:00', '18:30:00'), ('2', '2022-12-22', '2', '09:00:00', '18:30:00'), ('3', '2022-12-22', '3', '10:00:00', '19:30:00'), ('4', '2020-02-21', '1', '10:30:00', '20:30:00'), ('5', '2023-12-12', '2', '10:00:00', '21:30:00'), ('6', '2023-12-23', '3', '10:00:00', '22:30:00'), ('7', '2023-12-22', '3', '08:00:00', '17:30:00'), ('8', '2021-02-21', '2', '09:00:00', '18:30:00'), ('9', '2024-12-12', '3', '10:00:00', '19:30:00'), ('10', '2024-12-23', '1', '10:00:00', '20:30:00'), ('11', '2024-12-22', '2', '10:00:00', '21:30:00'), ('12', '2022-02-21', '3', '10:00:00', '22:30:00'), ('13', '2022-12-12', '1', '08:00:00', '17:30:00'), ('14', '2022-12-23', '3', '09:00:00', '18:30:00'), ('15', '2022-12-02', '2', '10:00:00', '19:30:00'), ('16', '2020-02-21', '1', '09:45:00', '22:00:00'), ('17', '2023-12-12', '2', '10:00:00', '21:30:00'), ('18', '2020-12-23', '3', '10:00:00', '22:30:00'), ('19', '2023-12-22', '1', '08:00:00', '17:30:00'), ('20', '2021-02-21', '2', '09:00:00', '18:30:00'), ('21', '2024-12-03', '2', '10:00:00', '19:30:00'), ('22', '2021-12-23', '1', '10:00:00', '20:30:00'), ('23', '2024-10-22', '3', '10:00:00', '21:30:00'), ('24', '2022-02-21', '3', '10:00:00', '22:30:00'), ('25', '2022-12-12', '1', '08:00:00', '17:30:00'), ('26', '2022-09-23', '2', '09:00:00', '18:30:00'), ('27', '2022-12-22', '4', '10:00:00', '19:30:00'), ('28', '2020-02-21', '1', '10:00:00', '20:30:00'), ('29', '2023-04-12', '5', '10:00:00', '21:30:00'), ('30', '2023-12-03', '4', '10:00:00', '22:30:00'), ('31', '2023-12-22', '1', '08:00:00', '17:30:00'), ('32', '2021-02-21', '5', '09:00:00', '18:30:00'), ('33', '2024-12-12', '4', '10:00:00', '19:30:00');
My current MySQL query which displays employees who are absent does not include the date when the employee is absent:
SELECT ci_admin.emp_no,
ci_admin.firstname,
ci_admin.lastname,
ci_admin.emp_photo,
ci_department.department_name,
ci_admin_roles.admin_role_title
FROM ci_admin
INNER JOIN ci_department ON ci_department.department_id = ci_admin.department_id
INNER JOIN ci_admin_roles on ci_admin_roles.admin_role_id=ci_admin.admin_role_id
WHERE ci_admin.is_deleted = 0
AND ci_admin.admin_id NOT IN (SELECT ci_attendance.admin_id
FROM ci_attendance
WHERE ci_attendance.attendance_date BETWEEN '2023-01-01' AND '2023-01-31')
ORDER BY ci_admin.department_id
I want the specific date that an employee is absent to be included in my query. How can I modify my query?

mysql select sum if value equal set sum to 0

I am developing wms software, I write SQL to calculate items stock in warehouse. in the table I record out and in stock values if go out then negative value if receive in the warehouse item value is positive. In the same table I record inventory control too. So, I need SQLquery with sum all items stock and if have inventory control with same id of items, then if found SUM expression an inventory control row , The SUM expression need to equal with inventory control value and the resume summing.
So when SUM expression run into control row, I need to sum from the latest control to a defined date
information:
-- Server version: 8.0.18
-- PHP Version: 7.3.11
Create table :
CREATE TABLE `magacin_zalihe` (
`magacin_zalihe_id` int(11) NOT NULL,
`magacin_zalihe_magacin` varchar(8) COLLATE utf8mb4_general_ci DEFAULT NULL,
`magacin_zalihe_materijali_id` int(11) NOT NULL,
`magacin_zalihe_materijali_sifra` varchar(100) COLLATE utf8mb4_general_ci NOT NULL,
`magacin_zalihe_materijali_naziv` varchar(100) COLLATE utf8mb4_general_ci NOT NULL,
`magacin_zalihe_materijali_kolicina` decimal(10,2) NOT NULL,
`magacin_zalihe_materijali_tip` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL,
`magacin_zalihe_materijali_tip2` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL,
`magacin_zalihe_materijali_mera` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL,
`magacin_zalihe_materijali_pakovanje` decimal(10,2) NOT NULL,
`magacin_zalihe_materijali_paleta` decimal(10,2) NOT NULL,
`magacin_zalihe_materijali_tezina` decimal(10,2) NOT NULL,
`magacin_zalihe_materijali_opis` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`magacin_zalihe_magacioner` varchar(100) COLLATE utf8mb4_general_ci NOT NULL,
`magacin_zalihe_datum` date NOT NULL,
`magacin_zalihe_narudzba` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`magacin_zalihe_vrsta` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL,
`magacin_zalihe_kor_modif` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL,
`magacin_zalihe_dat_modif` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
INSERT:
INSERT INTO `magacin_zalihe` (`magacin_zalihe_id`, `magacin_zalihe_magacin`, `magacin_zalihe_materijali_id`, `magacin_zalihe_materijali_sifra`, `magacin_zalihe_materijali_naziv`, `magacin_zalihe_materijali_kolicina`, `magacin_zalihe_materijali_tip`, `magacin_zalihe_materijali_tip2`, `magacin_zalihe_materijali_mera`, `magacin_zalihe_materijali_pakovanje`, `magacin_zalihe_materijali_paleta`, `magacin_zalihe_materijali_tezina`, `magacin_zalihe_materijali_opis`, `magacin_zalihe_magacioner`, `magacin_zalihe_datum`, `magacin_zalihe_narudzba`, `magacin_zalihe_vrsta`, `magacin_zalihe_kor_modif`, `magacin_zalihe_dat_modif`) VALUES
(24, '1000', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '200.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-05-20', '', 'utovar', 'rsolar', '2021-05-20 12:26:50'),
(25, '1000', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '250.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-05-20', '', 'utovar', 'rsolar', '2021-05-20 12:26:50'),
(26, '1000', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '50.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-05-20', '', 'utovar', 'rsolar', '2021-05-20 12:26:50'),
(27, '1000', 120, '1124314522021', 'POLIFLEX MINERAL P4,5', '-50.00', '2', '0', 'm2', '10.00', '23.00', '43.00', '', 'rsolar', '2021-05-19', '', 'istovar', 'rsolar', '2021-05-21 05:56:59'),
(28, '1000', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '-25.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-05-19', '', 'istovar', 'rsolar', '2021-05-21 05:56:59'),
(29, '1000', 120, '1124314522021', 'POLIFLEX MINERAL P4,5', '100.00', '2', '0', 'm2', '10.00', '23.00', '43.00', '', 'rsolar', '2021-05-21', '', 'utovar', 'rsolar', '2021-05-21 08:03:28'),
(30, '1000', 120, '1124314522021', 'POLIFLEX MINERAL P4,5', '50.00', '2', '0', 'm2', '10.00', '23.00', '43.00', '', 'rsolar', '2021-05-22', '', 'utovar', 'rsolar', '2021-05-21 08:05:10'),
(31, '1000', 120, '1124314522021', 'POLIFLEX MINERAL P4,5', '-100.00', '2', '0', 'm2', '10.00', '23.00', '43.00', '', 'dbence', '2021-05-19', '', 'istovar', 'dbence', '2021-05-21 08:25:14'),
(32, '1000', 120, '1124314522021', 'POLIFLEX MINERAL P4,5', '-80.00', '2', '0', 'm2', '10.00', '23.00', '43.00', '', 'dbence', '2021-05-21', '', 'istovar', 'dbence', '2021-05-21 08:31:13'),
(33, '1000', 52, '1122432441001', 'FIMAL A4/P', '-80.00', '2', '0', 'm2', '7.50', '20.00', '30.00', '', 'dbence', '2021-05-21', '', 'istovar', 'dbence', '2021-05-21 08:31:13'),
(34, '100', 125, '1124331321001', 'POLIFLEX PF4/S', '-100.00', '2', '0', 'm2', '10.00', '23.00', '44.00', '', 'rsolar', '2021-05-21', '', 'istovar', 'rsolar', '2021-05-21 08:57:27'),
(35, '1000', 121, '1123320261001', 'POLIFLEX P2', '-100.00', '2', '0', 'm2', '10.00', '30.00', '28.00', '', 'rsolar', '2021-05-21', '123456-2021', 'istovar', 'rsolar', '2021-05-21 12:09:24'),
(36, '1000', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '-400.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-05-21', '223564-2021', 'istovar', 'rsolar', '2021-05-21 12:10:11'),
(37, '126', 4, '1111152042050', 'BÁZIS FATA CSUPASZ', '100.00', '2', '0', 'm2', '20.00', '64.00', '9.00', '', 'rsolar', '2021-05-21', '', 'utovar', 'rsolar', '2021-05-21 12:51:55'),
(38, '100', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '10.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-05-26', '', 'utovar', 'rsolar', '2021-05-31 10:18:58'),
(39, '100', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '-100.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-05-31', '', 'istovar', 'rsolar', '2021-05-31 11:15:15'),
(40, '100', 24, '1110110002001', 'CARTABIT 350 1000M', '-150.00', '2', '0', 'm2', '1050.00', '1.00', '380.00', '', 'rsolar', '2021-05-31', '', 'istovar', 'rsolar', '2021-05-31 11:16:30'),
(41, '100', 131, '1111311852001', 'POLYESTER 260 P11', '10.00', '2', '0', 'm2', '13.50', '36.00', '25.90', '', 'rsolar', '2021-06-23', '', 'utovar', 'rsolar', '2021-07-05 06:12:45'),
(42, '100', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '200.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-06-23', 'INV - 2021-06-23', 'inventar', 'rsolar', '2021-07-05 06:12:45'),
(43, '100', 31, '1111141002007', 'CARTONFELTRO BITUMATO T350 2x1000M', '100.00', '2', '0', 'm2', '1050.00', '2.00', '370.00', '', 'rsolar', '2021-06-23', '', 'utovar', 'rsolar', '2021-07-05 06:12:45'),
(44, '100', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '-85.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-05-31', '', 'istovar', 'rsolar', '2021-07-05 06:13:07'),
(45, '100', 29, '1111141002005', 'CARTONFELTRO BITUMATO T 300 MARCEGAGLIA', '-1.00', '2', '0', 'm2', '1020.00', '1.00', '346.00', '', 'rsolar', '2021-05-31', '', 'istovar', 'rsolar', '2021-07-05 06:13:07'),
(46, '100', 120, '1124314522021', 'POLIFLEX MINERAL P4,5', '100.00', '2', '0', 'm2', '10.00', '23.00', '43.00', '', 'rsolar', '2021-07-05', '', 'utovar', 'rsolar', '2021-07-06 07:49:55');
First column as primary key and auto increment
needed output:
Output
in output needed 200 because the last row is control row and contains stock value of 200
my sql query:
SELECT SUM( t1.magacin_zalihe_materijali_kolicina ) AS temp,t1.* FROM magacin_zalihe AS t1 WHERE t1.magacin_zalihe_magacin='100' AND t1.magacin_zalihe_datum <= '2021-07-07' GROUP BY t1.magacin_zalihe_materijali_id
t1.magacin_zalihe_vrsta contains 1 row with value 'inventar' this is control for warehouse. From this value needed to sum stock values. If have more than 1 row of control we needed to sum from latest control row.
my sql query to try but not works:
SELECT SUM(IF( t1.magacin_zalihe_vrsta != 'inventar', t1.magacin_zalihe_materijali_kolicina, #temp := t1.magacin_zalihe_materijali_kolicina )) AS temp,t1.* FROM magacin_zalihe AS t1 WHERE t1.magacin_zalihe_magacin='100' AND t1.magacin_zalihe_datum <= '2021-07-07' GROUP BY t1.magacin_zalihe_materijali_id
Thanks,
Robert
SELECT IF (
p1.temp = 0,
p1.magacin_zalihe_materijali_kolicina,
p1.temp
) as temp_delta
FROM (
SELECT SUM(
IF(
t1.magacin_zalihe_vrsta != 'inventar',
0,
#temp := t1.magacin_zalihe_materijali_kolicina
)
) AS temp,
t1.*
FROM magacin_zalihe AS t1
WHERE t1.magacin_zalihe_magacin = '100'
AND t1.magacin_zalihe_datum <= '2021-07-07'
GROUP BY t1.magacin_zalihe_materijali_id
) as p1;

What should I change not to get error 1136 in MySQL?

CREATE TABLE IF NOT EXISTS `mushu`.`episode` (
`id_episode` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`number` VARCHAR(5) NOT NULL,
`name` VARCHAR(50) NOT NULL,
`episode_length` VARCHAR(10) NOT NULL,
`location` VARCHAR(150) NOT NULL,
`season_id_season` INT UNSIGNED NOT NULL,
`season_tv_show_id_tv_show` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id_episode`, `season_id_season`, `season_tv_show_id_tv_show`),
INDEX `fk_episode_season1_idx` (`season_id_season` ASC,
`season_tv_show_id_tv_show` ASC) VISIBLE,
CONSTRAINT `fk_episode_season1`
FOREIGN KEY (`season_id_season` , `season_tv_show_id_tv_show`)
REFERENCES `mushu`.`season` (`id_season` , `tv_show_id_tv_show`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
INSERT INTO episode VALUES
(1, '1', 'Pilot', '45 min', 'E:\MuShu', '1', '1'),
(2, '2', 'Another Trip Around the Sun', '45 min', 'E:\MuShu', '1', '1'),
(3, '1', 'Pilot', '43 min', 'E:\MuShu', '1', '2'),
(4, '2', 'A Stray Howl', '43 min', 'E:\MuShu', '1', '2'),
(5, '3', 'Eight Slim Grins', '43 min', 'E:\MuShu', '1', '2'),
(6, '1', 'Burnt Food', '42 min', 'E:\MuShu', '1', '3'),
(7, '2', 'Mount Rushmore', '42 min', 'E:\MuShu', '1', '3'),
(8, '1', 'Impact', '56 min', 'E:\MuShu', '1', '4'),
(9, '1', 'Pilot', '44 min', 'E:\MuShu', '1', '5'),
(10, '2', 'The Lorelais First Day at Chilton', '44 min', 'E:\MuShu', '1', '5'),
(11, '1', 'Pilot', '42 min', 'E:\MuShu', '1', '6'),
(12, '2', 'Independence Day', '42 min', 'E:\MuShu', '1', '6'),
(13, '3', 'Comrades in Arms', '42 min', 'E:\MuShu', '1', '6'),
(14, '1', 'Pilot', '22 min', 'E:\MuShu', '1', '7'),
(15, '2', 'Rockets, Communists and the Dewey Decimal System', '22 min', 'E:\MuShu', '1', '7'),
(16, '1', 'Always and Forever', '45 min', '1', '8'),
(17, '1', 'Pilot', '43 min', '1', '9'),
(18, '2', 'Its All Her Fault', '1', '9'),
(19, '1', 'Pilot', '41 min', '1', '10'),
(20, '2', 'The Jenna Thing', '41 min', '1', '10');
Error Code: 1136. Column count doesn't match value count at row 16.
How do I solve this error?
Some values a 7 long others are 6. They all must be the same (7). If you look at row 16 like the error says you'll see.

Get the the product with highest sells in each store - keep getting wrong products but right quantities

For the following table I want to get a table that shows the most sold product in each store
Same product can be the most sold in more than 1 store.
This is the query I tried:
SELECT StoreID, ProductID, MAX(Quantity), TotalPrice FROM MyTable GROUP BY StoreID LIMIT 0, 99999;
The answer I get is:
|StoreID|ProductID|MAX(Quantity)|TotalPrice
'1', '1', '566', '288.0000'
'2', '1', '782', '648.0000'
'3', '1', '473', '172.8000'
The problem:
I've checked and the max quantities are correct
but for some reason it shows the ID '1' for every product even that it's not the most sold product in any store.
For this table (Called 'MyTable'):
|StoreID|TotalPrice|ProductID|Quantity
'1', '288.0000', '1', '167'
'1', '760.0000', '2', '417'
'1', '400.0000', '3', '70'
'1', '352.0000', '4', '218'
'1', '1105.0000', '5', '117'
'1', '600.0000', '6', '156'
'1', '300.0000', '7', '193'
'1', '960.0000', '8', '84'
'1', '1552.0000', '9', '36'
'1', '558.0000', '10', '115'
'1', '201.6000', '11', '218'
'1', '152.0000', '12', '8'
'1', '9.6000', '13', '187'
'1', '167.4000', '14', '47'
'1', '62.0000', '15', '42'
'1', '417.0000', '16', '451'
'1', '936.0000', '17', '345'
'1', '450.0000', '18', '112'
'1', '7.3000', '19', '68'
'1', '1701.0000', '20', '66'
'1', '80.0000', '21', '337'
'1', '100.8000', '22', '58'
'1', '151.2000', '23', '275'
'1', '43.2000', '24', '431'
'1', '44.8000', '25', '171'
'1', '1245.0000', '26', '297'
'1', '526.5000', '27', '185'
'1', '1528.8000', '28', '127'
'1', '1485.0000', '29', '61'
'1', '124.2000', '30', '158'
'1', '600.0000', '31', '566'
'1', '153.6000', '32', '91'
'1', '120.0000', '33', '294'
'1', '112.0000', '34', '62'
'1', '57.6000', '35', '249'
'1', '456.0000', '36', '200'
'1', '208.0000', '37', '18'
'1', '4216.0000', '38', '86'
'1', '57.6000', '39', '192'
'1', '735.0000', '40', '431'
'1', '123.2000', '41', '220'
'1', '22.4000', '42', '183'
'1', '920.0000', '43', '141'
'1', '325.5000', '44', '281'
'1', '114.0000', '45', '360'
'1', '288.0000', '46', '249'
'1', '418.0000', '47', '156'
'1', '191.2500', '48', '45'
'1', '240.0000', '49', '45'
'1', '406.2500', '50', '25'
'1', '1696.0000', '51', '271'
'1', '112.0000', '52', '146'
'1', '1048.0000', '53', '202'
'1', '59.0000', '54', '180'
'1', '384.0000', '55', '147'
'1', '2128.0000', '56', '428'
'1', '234.0000', '57', '146'
'1', '318.0000', '58', '201'
'1', '3080.0000', '59', '504'
'1', '544.0000', '60', '433'
'1', '364.8000', '61', '221'
'1', '591.0000', '62', '247'
'1', '2808.0000', '63', '187'
'1', '1330.0000', '64', '194'
'1', '336.0000', '65', '250'
'1', '816.0000', '66', '110'
'1', '56.0000', '67', '105'
'1', '30.0000', '68', '245'
'1', '432.0000', '69', '305'
'1', '252.0000', '70', '254'
'1', '344.0000', '71', '326'
'1', '556.0000', '72', '400'
'1', '240.0000', '73', '143'
'1', '400.0000', '74', '70'
'1', '186.0000', '75', '490'
'1', '216.0000', '76', '348'
'1', '104.0000', '77', '255'
'2', '648.0000', '1', '317'
'2', '608.0000', '2', '453'
'2', '160.0000', '3', '128'
'2', '422.4000', '4', '109'
'2', '85.4000', '5', '124'
'2', '150.0000', '6', '77'
'2', '240.0000', '7', '332'
'2', '2240.0000', '8', '222'
'2', '4850.0000', '9', '53'
'2', '396.8000', '10', '435'
'2', '403.2000', '11', '293'
'2', '570.0000', '12', '107'
'2', '96.0000', '13', '477'
'2', '279.0000', '14', '224'
'2', '155.0000', '15', '60'
'2', '139.0000', '16', '294'
'2', '468.0000', '17', '416'
'2', '2000.0000', '18', '218'
'2', '109.5000', '19', '422'
'2', '2592.0000', '20', '219'
'2', '160.0000', '21', '286'
'2', '201.6000', '22', '204'
'2', '180.0000', '23', '187'
'2', '54.0000', '24', '363'
'2', '134.4000', '25', '120'
'2', '249.0000', '26', '164'
'2', '1755.0000', '27', '80'
'2', '1019.2000', '28', '281'
'2', '1980.0000', '29', '458'
'2', '165.6000', '30', '175'
'2', '200.0000', '31', '533'
'2', '1024.0000', '32', '176'
'2', '50.0000', '33', '233'
'2', '156.8000', '34', '158'
'2', '288.0000', '35', '348'
'2', '608.0000', '36', '359'
'2', '468.0000', '37', '18'
'2', '4216.0000', '38', '292'
'2', '604.8000', '39', '317'
'2', '588.0000', '40', '518'
'2', '77.0000', '41', '301'
'2', '112.0000', '42', '223'
'2', '552.0000', '43', '298'
'2', '248.0000', '44', '136'
'2', '380.0000', '45', '40'
'2', '48.0000', '46', '230'
'2', '121.6000', '47', '197'
'2', '153.0000', '48', '15'
'2', '640.0000', '49', '344'
'2', '195.0000', '50', '101'
'2', '1484.0000', '51', '328'
'2', '44.8000', '52', '198'
'2', '393.0000', '53', '320'
'2', '59.0000', '54', '295'
'2', '403.2000', '55', '283'
'2', '121.6000', '56', '486'
'2', '249.6000', '57', '202'
'2', '318.0000', '58', '102'
'2', '660.0000', '59', '630'
'2', '1088.0000', '60', '782'
'2', '2052.0000', '61', '177'
'2', '591.0000', '62', '399'
'2', '280.8000', '63', '223'
'2', '931.0000', '64', '397'
'2', '252.0000', '65', '229'
'2', '408.0000', '66', '95'
'2', '210.0000', '67', '55'
'2', '200.0000', '68', '298'
'2', '518.4000', '69', '232'
'2', '240.0000', '70', '299'
'2', '34.4000', '71', '258'
'2', '667.2000', '72', '200'
'2', '300.0000', '73', '85'
'2', '168.0000', '74', '92'
'2', '37.2000', '75', '542'
'2', '86.4000', '76', '367'
'2', '124.8000', '77', '236'
'3', '172.8000', '1', '344'
'3', '304.0000', '2', '187'
'3', '240.0000', '3', '130'
'3', '1100.0000', '4', '126'
'3', '204.0000', '5', '57'
'3', '120.0000', '6', '68'
'3', '360.0000', '7', '238'
'3', '400.0000', '8', '66'
'3', '582.0000', '9', '6'
'3', '595.2000', '10', '192'
'3', '168.0000', '11', '195'
'3', '364.8000', '12', '229'
'3', '48.0000', '13', '227'
'3', '167.4000', '14', '133'
'3', '248.0000', '15', '20'
'3', '486.5000', '16', '413'
'3', '249.6000', '17', '217'
'3', '600.0000', '18', '209'
'3', '131.4000', '19', '233'
'3', '1814.4000', '20', '28'
'3', '80.0000', '21', '393'
'3', '840.0000', '22', '86'
'3', '288.0000', '23', '118'
'3', '100.8000', '24', '331'
'3', '112.0000', '25', '27'
'3', '597.6000', '26', '292'
'3', '877.5000', '27', '100'
'3', '728.0000', '28', '232'
'3', '990.0000', '29', '227'
'3', '1242.0000', '30', '279'
'3', '150.0000', '31', '298'
'3', '320.0000', '32', '30'
'3', '40.0000', '33', '228'
'3', '224.0000', '34', '286'
'3', '1440.0000', '35', '286'
'3', '380.0000', '36', '246'
'3', '20.8000', '37', '89'
'3', '10540.0000', '38', '245'
'3', '86.4000', '39', '284'
'3', '882.0000', '40', '154'
'3', '192.5000', '41', '460'
'3', '98.0000', '42', '291'
'3', '1472.0000', '43', '141'
'3', '620.0000', '44', '184'
'3', '228.0000', '45', '108'
'3', '144.0000', '46', '69'
'3', '114.0000', '47', '132'
'3', '714.0000', '48', '78'
'3', '560.0000', '49', '131'
'3', '195.0000', '50', '109'
'3', '127.2000', '51', '287'
'3', '112.0000', '52', '156'
'3', '262.0000', '53', '200'
'3', '29.5000', '54', '280'
'3', '2304.0000', '55', '473'
'3', '60.8000', '56', '349'
'3', '312.0000', '57', '86'
'3', '848.0000', '58', '231'
'3', '1320.0000', '59', '362'
'3', '952.0000', '60', '362'
'3', '712.5000', '61', '205'
'3', '472.8000', '62', '437'
'3', '175.5000', '63', '35'
'3', '239.4000', '64', '149'
'3', '672.0000', '65', '266'
'3', '136.0000', '66', '34'
'3', '336.0000', '67', '24'
'3', '100.0000', '68', '256'
'3', '28.8000', '69', '177'
'3', '60.0000', '70', '264'
'3', '516.0000', '71', '473'
'3', '174.0000', '72', '206'
'3', '525.0000', '73', '65'
'3', '288.0000', '74', '135'
'3', '37.2000', '75', '123'
'3', '475.2000', '76', '266'
'3', '156.0000', '77', '300'
Try something like below:
SELECT *
FROM MyTable WHERE (storeID,Quantity) IN
( SELECT storeID, MAX(Quantity)
FROM MyTable
GROUP BY storeID
)
The query you wrote is ambiguous. ProductID is not the product that had the max quantity. Think about this query:
SELECT StoreID, ProductID, MAX(Quantity), MIN(Quantity), TotalPrice
FROM MyTable
GROUP BY StoreID LIMIT 0, 99999;
Which ProductID do you expect this query to return? The product that has the max quantity or the product that has the min quantity?
Or this:
SELECT StoreID, ProductID, AVG(Quantity), TotalPrice
FROM MyTable
GROUP BY StoreID LIMIT 0, 99999;
Perhaps no product has a quantity that is exactly the average quantity.
This type of SQL query is technically illegal and it's an error in most databases other than MySQL. MySQL allows it, and trusts you to know when you're writing such an ambiguous query.
Tip: Every column of your select-list must be either:
Inside an aggregate function
Named in the GROUP BY clause.
Any other column will not return a reliable value.
You're asking for a query that has been dubbed the greatest-n-per-group problem. This question comes up frequently on Stack Overflow, and it has been answered many times. Here's a couple of my past answers that got a lot of upvotes:
Retrieving the last record in each group
Fetch the row which has the Max value for a column
It matches because you're grouping by StoreID. MAX is a GROUP BY function, so it returns the MAX(Quantity) for each StoreID.
For ProductID and TotalPrice, the query is just returning the values from the first row for each store, because there's nothing telling it to read the values from the row where Quantity=MAX(Quantity)
TheUknown's query, on the other hand, first finds the MAX(Quantity) for each store, then does a second query to return all the fields from the row where Quantity=MAX(Quantity)

get all the other user names where user=1 exists

I have the following "tblA" and "tblB" table:
CREATE TABLE if not exists tblA
(
id int(11) NOT NULL auto_increment,
sender varchar(255),
receiver varchar(255),
msg varchar(255),
date timestamp,
PRIMARY KEY (id)
);
CREATE TABLE if not exists tblB
(
id int(11) NOT NULL auto_increment,
sno varchar(255),
name varchar(255),
PRIMARY KEY (id)
);
INSERT INTO tblA (sender, receiver, msg,date) VALUES
('1', '2', 'buzz ...','2011-08-21 14:11:09'),
('1', '2', 'test ...','2011-08-21 14:12:19'),
('1', '2', 'check ...','2011-08-21 14:13:29'),
('1', '1', 'test2 ...','2011-08-21 14:14:09'),
('2', '1', 'check2 ...','2011-08-21 14:15:09'),
('2', '1', 'test3 ...','2011-08-21 14:16:09'),
('1', '2', 'buzz ...','2011-08-21 14:17:09'),
('1', '2', 'test ...','2011-08-21 14:18:19'),
('1', '2', 'check ...','2011-08-21 14:19:29'),
('1', '1', 'test2 ...','2011-08-21 14:10:09'),
('3', '1', 'check2 ...','2011-08-21 14:21:09'),
('3', '1', 'test3 ...','2011-08-21 14:22:09'),
('3', '2', 'buzz ...','2011-08-21 14:24:09'),
('3', '2', 'test ...','2011-08-21 14:25:19'),
('1', '3', 'check ...','2011-08-21 14:26:29'),
('1', '3', 'test2 ...','2011-08-21 14:27:09'),
('2', '3', 'check2 ...','2011-08-21 14:28:09'),
('2', '3', 'test3 ...','2011-08-21 14:29:09'),
('1', '2', 'check3 ...','2011-08-21 14:23:09'),
('1', '4', 'test2 ...','2011-08-21 14:27:09'),
('1', '5', 'test2 ...','2011-08-21 14:27:09'),
('2', '6', 'check2 ...','2011-08-21 14:28:09'),
('2', '7', 'test3 ...','2011-08-21 14:29:09'),
('8', '2', 'check3 ...','2011-08-21 14:23:09');
INSERT INTO tblB (sno, name) VALUES
('1', 'Aa'),
('2', 'Bb'),
('3', 'Cc'),
('4', 'Dd'),
('5', 'Ee'),
('6', 'Ff'),
('7', 'Gg'),
('8', 'Hh');
How to get all rows with user : 1existing along with the name of sender/receiver which is not1 (ie:Aa)
I think this will get the unique list:
select distinct b.name
from tblA a join
tblB b
on b.id in (a.sender, a.receiver) and
((b.id <> 1 and (a.sender = 1 or a.receiver = 1)) or
(a.sender = 1 and a.receiver = 1)
);
SELECT ta.*, tb.name
FROM tablA ta, tablB tb WHERE (ta.sender = tb.sno OR ta.receiver = tb.sno)
AND tb.sno <> 1