MYSQL - Count consecutive identical values - mysql

I have a MySQL table which basically stores a list of points with reference of the player id who made that point.
My setup is:
CREATE TABLE `points` (
`id` int(10) unsigned NOT NULL,
`player_id` int(10) unsigned NOT NULL,
`game_id` int(10) unsigned NOT NULL,
`score` int(11) NOT NULL,
`isScore` tinyint(1) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
);
INSERT INTO `points` (`id`, `player_id`, `game_id`,
`score`, `isScore`, `created_at`, `updated_at`)
VALUES
(16, 11, 60, 4, 0, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(17, 11, 60, 5, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(18, 12, 60, 6, 0, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(19, 12, 60, 7, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(20, 12, 60, 8, 0, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(21, 12, 60, 9, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(22, 8, 60, 10, 0, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(23, 8, 60, 11, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(24, 12, 60, 12, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(25, 12, 60, 13, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(26, 5, 60, 14, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(27, 12, 60, 15, 0, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(28, 12, 60, 16, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(29, 10, 60, 17, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39'),
(30, 10, 60, 18, 1, '2016-12-28 12:19:39', '2016-12-28 12:19:39');
Here's a SQLfiddle with this setup: http://sqlfiddle.com/#!9/f6ca9f8
I want to get the number of maximum consecutive point made by a player for each player.
So in the sqlfiddle the result will be something like:
+-----------+------------------------+
| player_id | max_consegutive_points |
+-----------+------------------------+
| 12 | 4 |
| 8 | 2 |
| 5 | 1 |
| 10 | 2 |
+-----------+------------------------+

Your question is missing some key points (no pun intended). For example: what is considered a consecutive identical value? You need to define if those values will be listed ordered by ID, created_at, ID+game_id, etc.
So let's say the order is given by the ID, because it may be autoincremental and it's only natural that points are inserted in the order they happened.
However, the answer will be to create a query with a counter variable (accum) that increases if the player is the same than the one in the former row, or resets to 1 if the player changes:
select
points.id,
points.player_id,
points.game_id,
points.created_at,
#counter:=IF(#playerid=points.player_id,#counter+1,1) as accum,
#playerid:=points.player_id as current_player_id
from points,
(select #counter:=1) c,
(select #playerid:=0) p
order by points.id
See SQLFiddle here
Having the result of that table, and putting it into a view or temporary table that we may call "accums", you just need to do
SELECT player_id, max(accum)
FROM accums
GROUP BY player_id

select distinct(player_id), MAX(score) from points group by player_id;

Related

OR operator applying to entire WHERE clause

SELECT DISTINCT
customers.customer_id,
services.name
FROM users
INNER JOIN customers ON users.user_id=customers.user_id
LEFT JOIN appointments ON customers.customer_id=appointments.customer_id
INNER JOIN pets ON customers.customer_id=pets.customer_id
INNER JOIN services on appointments.service_id=services.service_id
WHERE ((appointments.customer_id IS NULL)
OR NOT (appointments.date > (SELECT SUBDATE(CURDATE(), 365)))
OR ((appointments.date > (SELECT SUBDATE(CURDATE(), 365)))
AND services.name NOT LIKE '%General Health Checkup%'))
GROUP BY customers.customer_id
I am trying to find all customers who are due a yearly general health checkup
this requires them to have:
a) never have appointments
b) not had an appointment in the past year
c) had an appointment in the past year but it wasn't a general health checkup
I assumed the final OR im my WHERE clause would only apply to that that operation i.e.
OR ((appointments.date > (SELECT SUBDATE(CURDATE(), 365))) AND services.name NOT LIKE '%General Health Checkup%'))
however it is ruling out all options for services named General Health Checkup.
How can i only apply this to that area of the where clause so that general health check up appointments can get through but only if they are from over a year ago.
Thanks !
appointments table:
CREATE TABLE `appointments` (
`appointment_id` int(8) NOT NULL,
`customer_id` int(8) DEFAULT NULL,
`service_id` int(4) DEFAULT NULL,
`staff_id` int(6) DEFAULT NULL,
`pet_id` int(9) DEFAULT NULL,
`date` date NOT NULL,
`start_time` time NOT NULL,
`status` enum('Open','Cancelled','Completed','') NOT NULL,
`create_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `appointments`
--
INSERT INTO `appointments` (`appointment_id`, `customer_id`, `service_id`, `staff_id`, `pet_id`, `date`, `start_time`, `status`, `create_date`) VALUES
(1, 1, 2, 1, 1, '2017-03-22', '10:00:00', 'Completed', '2022-03-16 11:28:46'),
(2, 3, 2, 1, 6, '2021-06-18', '12:00:00', 'Completed', '2021-06-15 11:01:43'),
(3, 2, 2, 1, 2, '2020-07-17', '13:00:00', 'Completed', '2020-05-14 11:30:18'),
(4, 3, 2, 1, 5, '2020-07-10', '14:00:00', 'Completed', '2020-05-21 11:30:18'),
(5, 4, 3, 1, 7, '2020-09-17', '10:00:00', 'Completed', '2022-03-16 12:31:59'),
(6, 8, 2, 1, 11, '2022-03-17', '12:00:00', 'Cancelled', '2022-03-17 23:44:56'),
(7, 4, 2, 7, 7, '2022-03-17', '10:00:00', 'Cancelled', '2022-03-17 23:50:11'),
(8, 1, 1, 13, 1, '2022-03-17', '13:00:00', 'Completed', '2022-03-18 00:28:10'),
(9, 7, 2, 13, 9, '2022-03-18', '15:00:00', 'Cancelled', '2022-03-18 13:16:37'),
(10, 7, 1, 13, 10, '2022-03-18', '16:00:00', 'Cancelled', '2022-03-18 13:48:12'),
(11, 1, 1, 13, 1, '2022-03-22', '11:00:00', 'Completed', '2022-03-22 12:34:55'),
(12, 11, 1, 13, 11, '2022-03-23', '13:00:00', 'Completed', '2022-03-23 15:28:22'),
(13, 9, 3, 13, 12, '2022-03-26', '13:00:00', 'Completed', '2022-03-26 13:13:46'),
(14, 35, 2, 13, 16, '2022-03-27', '10:00:00', 'Completed', '2022-03-27 16:09:14'),
(15, 34, 2, 13, 20, '2022-03-28', '10:00:00', 'Completed', '2022-03-28 10:05:41'),
(16, 33, 1, 13, 20, '2022-03-28', '12:00:00', 'Completed', '2022-03-28 11:40:50'),
(17, 8, 2, 13, 20, '2022-03-16', '14:00:00', 'Completed', '2022-03-28 12:31:42'),
(18, 15, 2, 13, 20, '2022-03-28', '14:00:00', 'Completed', '2022-03-28 12:33:47'),
(19, 31, 4, 13, 20, '2022-03-29', '00:00:00', 'Completed', '2022-03-29 14:20:04'),
(20, 31, 4, 13, 20, '2022-03-29', '10:00:00', 'Completed', '2022-03-29 14:20:42'),
(21, 1, 1, 13, 1, '2022-03-30', '11:00:00', 'Completed', '2022-03-30 15:18:23'),
(22, 33, 4, 13, 22, '2022-03-30', '12:00:00', 'Completed', '2022-03-30 15:22:02'),
(23, 3, 1, 13, 5, '2022-03-30', '13:00:00', 'Open', '2022-03-30 15:22:02'),
(24, 4, 1, 13, 7, '2022-03-30', '13:30:00', 'Completed', '2022-03-30 15:24:52'),
(25, 7, 2, 13, 10, '2022-03-30', '14:30:00', 'Open', '2022-03-30 15:26:11'),
(26, 12, 1, 7, 8, '2022-04-21', '10:00:00', 'Open', '2022-04-21 12:54:10'),
(27, 2, 1, 1, 8, '2022-04-21', '10:00:00', 'Cancelled', '2022-04-21 13:16:23'),
(28, 17, 4, 1, 20, '2022-04-21', '10:00:00', 'Completed', '2022-04-21 13:18:41'),
(29, NULL, 6, 7, 21, '2022-04-21', '12:00:00', 'Completed', '2022-04-21 16:22:44'),
(30, 13, 2, 3, 5, '2022-04-21', '14:00:00', 'Open', '2022-04-21 17:42:10'),
(31, 5, 1, 9, 14, '2022-04-22', '11:00:00', 'Open', '2022-04-22 16:16:57');
services table:
CREATE TABLE `services` (
`service_id` int(4) NOT NULL,
`name` varchar(100) NOT NULL,
`description` text NOT NULL,
`average_time` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `services`
--
INSERT INTO `services` (`service_id`, `name`, `description`, `average_time`) VALUES
(1, 'Consultation', 'General consultation to help you find the best path to good health for your pet.', 30),
(2, 'General Health Checkup', 'Review of your pets health.', 30),
(3, 'Microchip Insertion', 'Keep your dog safe and trackable with microchip.', 90),
(4, 'Puppy Vaccination', 'Initial puppy vaccination', 30),
(6, 'Booster Vaccination', 'Regular booster vaccincation service.', 30);
I would just check if the customer had a service_id=2 in the last year:
SELECT
customers.customer_id,
GROUP_CONCAT(CONCAT(services.name,'(',appointments.date,')')) as services
FROM users
INNER JOIN customers ON users.user_id=customers.user_id
LEFT JOIN appointments ON customers.customer_id=appointments.customer_id
INNER JOIN pets ON customers.customer_id=pets.customer_id
INNER JOIN services on appointments.service_id=services.service_id
WHERE SUBDATE(CURDATE(), 365) > (SELECT MAX(date)
FROM appointments
WHERE customers.customer_id=appointments.customer_id
AND appointments.service_id=2)
GROUP BY customers.customer_id
output:
customer_id
services
1
General Health Checkup(2017-03-22),Consultation(2022-03-30),Consultation(2022-03-22),Consultation(2022-03-17)
2
General Health Checkup(2020-07-17),Consultation(2022-04-21)
see: DBFIDDLE

Sum of values of a field based on another field

I have a table as follows. I need to build a query to find the following.
(Sum of feeComponentValue where feeComponentCalc = 1) - (Sum of feeComponentValue where feeComponentCalc = 1) and it should be group by feeSettingId
Please help
CREATE TABLE `feevalues` (
`feevaluesId` int(11) NOT NULL,
`feeSettingId` int(11) NOT NULL,
`feeComponentId` int(11) NOT NULL,
`feeComponentValue` int(11) DEFAULT NULL,
`feeComponentCalc` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table feevalues
INSERT INTO `feevalues`
(`feevaluesId`, `feeSettingId`, `feeComponentId`, `feeComponentValue`, `feeComponentCalc`)
VALUES
(27, 29, 1, 1000, 1),
(28, 29, 12, 2000, 1),
(29, 29, 3, 3000, 1),
(30, 29, 4, 4000, 1),
(103, 30, 3, 1000, 1),
(104, 30, 1, 2000, 1),
(105, 30, 3, 3000, 1),
(106, 30, 1, 4000, 1),
(107, 30, 14, 5000, 2),
(108, 30, 2, 6000, 2),
(109, 30, 13, 7000, 2),
(110, 30, 2, 8000, 2),
(111, 31, 1, 1000, 1),
(112, 31, 3, 2000, 1),
(113, 31, 13, 4000, 2),
(114, 31, 2, 3000, 2),
(122, 32, 1, 1, 1),
(123, 32, 3, 3, 1),
(124, 32, 4, 4, 1),
(125, 32, 2, 5, 2),
(126, 32, 14, 6, 2),
(127, 32, 13, 7, 2);
--
I assume you mean ...where feeComponentCalc = 2... in the 2nd case, right?
With conditional aggregation:
select
feeSettingId,
sum(case feeComponentCalc when 1 then feeComponentValue else 0 end) -
sum(case feeComponentCalc when 2 then feeComponentValue else 0 end) as result
from feevalues
group by feeSettingId
See the demo.
Or:
select
feeSettingId,
sum(
case feeComponentCalc
when 1 then feeComponentValue
when 2 then -feeComponentValue
else 0
end
) as result
from feevalues
group by feeSettingId
See the demo.
Results:
| feeSettingId | result |
| ------------ | ------ |
| 29 | 10000 |
| 30 | -16000 |
| 31 | -4000 |
| 32 | -10 |

mysql consult in same table

i have this table
INSERT INTO `relationships` (`id`, `term_id`, `order`) VALUES
(1, 1, 0),
(4, 2, 0),
(4, 3, 0),
(4, 4, 0),
(4, 5, 0),
(4, 7, 0),
(4, 8, 0),
(6, 3, 0),
(6, 8, 0),
(6, 9, 0),
(8, 6, 0),
(8, 7, 0),
(8, 8, 0),
(8, 10, 0),
(15, 3, 0),
(15, 4, 0),
(15, 10, 0);
I need to select all term_id containing the number 8 and 5 (relationship between this numbers)
expected result = id 4 and id 8
I try this
SELECT `id` FROM `relationships` WHERE `term_id`=8 or `term_id`=5
but selects all term_id belong to 8 and 5
result = id 4 id 8 AND id --> 6 <-- This is not the result I want.
This does the job:
SELECT
id
FROM relationships a
WHERE
a.term_id = 5 AND
EXISTS (SELECT * FROM relationships b WHERE b.term_id = 8 AND b.id = a.id) > 0
demo: http://www.sqlfiddle.com/#!9/e78cc/4/0

SELECT query for TableA and TableB IF TableB.col value (0 OR 1) I create new columns for 0 and 1

I have a Table pictures
create table pictures (
picture_id int(10) unsigned NOT NULL AUTO_INCREMENT,
mall_id float NOT NULL,
shop_id float NOT NULL,
picture_islogo int(11) NOT NULL ,
picture_path varchar(255) NOT NULL,
PRIMARY KEY (picture_id)
) ;
Sample Data for pictures
INSERT INTO pictures VALUES
(1, -1, 1, 1, 'photo.jpg'),
(2, -1, 2, 1, 'photo.jpg'),
(3, -1, 3, 1, 'photo.jpg'),
(4, -1, 4, 1, 'photo.jpg'),
(5, -1, 5, 1, 'photo.jpg'),
(6, -1, 6, 1, 'photo.jpg'),
(7, -1, 7, 1, 'photo.jpg'),
(8, -1, 8, 1, 'photo.jpg'),
(9, -1, 9, 1, 'photo.jpg'),
(10, -1, 10, 1, 'photo.jpg'),
(11, -1, 11, 1, 'photo.jpg'),
(12, -1, 12, 1, 'photo.jpg'),
(13, -1, 13, 1, 'photo.jpg'),
(14, -1, 13, 0, 'photo.jpg');
and other table malls
create table malls (
mall_id float NOT NULL AUTO_INCREMENT,
user_id float NOT NULL,
mall_displaysname varchar(255) NOT NULL,
mall_description text NOT NULL,
mall_contact varchar(14) NOT NULL,
mall_logo_picture_id int(11) NOT NULL,
mall_background_picture_id int(11) NOT NULL,
PRIMARY KEY (mall_id)
);
Sample Data for malls
INSERT INTO malls VALUES
(1, 2, 'mall', 'description', '+60 12 3456789', 14, 36),
(2, 5, 'mall 2', 'description', '+60 12 3456789', 15, 37),
(3, 6, 'mall 3', 'description ', '+60 12 3456789', 16, 38),
(4, 13, 'Multimedia University', 'description ', '+60 12 3456789', 17, 39),
(5, 18, 'Setia Walk', 'description ', '+60 12 3456789', 18, 40),
(6, 20, 'Ampang Point', 'description ', '+60 12 3456789', 19, 41),
(7, 21, 'Alamanda Plaza', 'description ', '+60 12 3456789', 20, 42),
(8, 22, 'Subang Parade', 'description ', '+60 12 3456789', 21, 43),
(9, 26, 'مجمع العرب - جدة', 'description ', '+60 12 3456789', 22, 44);
Where mall_background_picture_id and mall_background_picture_id reference to pictures.picture_id and Depend on the value of pictures.picture_islogo (0: Logo, 1:background or others)
i want a query to that returns the pictures.picture_path depends on the value of pictures.picture_islogo (0: Logo, 1:background or others)
the data that I need to get
mall_id , mall_displaysname, mall_logo, mall_background , mall_description, and mall_contact
Where mall_logo is the photo_path of the condition
pictures.picture_islogo = 1
and same thing with **mall_background** but
picture_islogo = 0
AND
mall_logo_picture_id = picture_id
Put the extra condition into the join condition:
select * -- you can chose what columns you want
from malls m
left join pictures pl on m.mall_logo_picture_id = pl.picture_id
and pl.picture_isLogo = 1
left join pictures pb on m.mall_background_picture_id = pb.picture_id
and pb.picture_isLogo = 0
To have left joins, the extra conditions must be in the join condition, which is evaluated as the join is being made.
If you put them in the where clause, which is evaluated after the join is made, you force the outer join to be an inner join, because the join must be successful fur the extra condition to be true.

mysql inner join with 3 tables issue with order count

I have the following sample tables.
CREATE TABLE IF NOT EXISTS `my_customer` (
`customer_id` int(11) NOT NULL AUTO_INCREMENT,
`customer_email` text NOT NULL,
PRIMARY KEY (`customer_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=64 ;
INSERT INTO `my_customer` (`customer_id`, `customer_email`) VALUES
(4, 'muthu.d#test.in'),
(5, 'nsrirengan#test.in'),
(6, 'vinothini.k#test.in'),
(8, 'vinothini.k111#test.in'),
(63, 'sri.n321#test.in'),
(56, 'vesri.n#test.in'),
(57, 'veesri.n#test.in'),
(58, 'veeisri.n#test.in'),
(59, 'ren#test.in'),
(60, 'ren1#test.in'),
(61, 'nsrirengan123#test.in'),
(62, 'nsrirengan321#test.in'),
(53, 'sri.n#test.in'),
(54, 'royalrenga#test.in'),
(55, 'vesri#test.in');
CREATE TABLE IF NOT EXISTS `my_order` (
`orderid` int(11) NOT NULL AUTO_INCREMENT,
`ordergenerateid` varchar(20) NOT NULL,
`restaurant_id` int(11) NOT NULL,
`customer_id` int(11) NOT NULL,
`usertype` varchar(10) NOT NULL,
`customeremail` varchar(200) NOT NULL,
PRIMARY KEY (`orderid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=152 ;
INSERT INTO `my_order` (`orderid`, `ordergenerateid`, `restaurant_id`, `customer_id`, `usertype`, `customeremail`) VALUES
(1, 'ORD0001', 3, 6, 'C', 'vinothini.k#test.in'),
(2, 'ORD0002', 1, 6, 'C', 'vinothini.k#test.in'),
(3, 'ORD0003', 3, 6, 'C', 'vinothini.k#test.in'),
(4, 'ORD0004', 3, 6, 'C', 'vinothini.k#test.in'),
(5, 'ORD0005', 3, 0, 'G', 'vinothini.k5555555#test.in'),
(6, 'ORD0006', 3, 6, 'C', 'vinothini.k#test.in'),
(7, 'ORD0007', 3, 6, 'C', 'vinothini.k#test.in'),
(8, 'ORD0008', 3, 6, 'C', 'vinothini.k#test.in'),
(9, 'ORD0009', 3, 6, 'C', 'vinothini.k#test.in'),
(10, 'ORD0010', 3, 6, 'C', 'vinothini.k#test.in'),
(11, 'ORD0011', 3, 6, 'C', 'vinothini.k#test.in'),
(12, 'ORD0012', 3, 6, 'C', 'vinothini.k#test.in'),
(13, 'ORD0013', 3, 6, 'C', 'vinothini.k#test.in'),
(14, 'ORD0014', 3, 6, 'C', 'vinothini.k#test.in'),
(15, 'ORD0015', 2, 3, 'C', 'sri.n#test.in'),
(16, 'ORD0016', 2, 3, 'C', 'sri.n#test.in'),
(17, 'ORD0017', 2, 3, 'C', 'sri.n#test.in'),
(18, 'ORD0018', 2, 3, 'C', 'sri.n#test.in'),
(19, 'ORD0019', 2, 3, 'C', 'sri.n#test.in'),
(20, 'ORD0020', 8, 3, 'C', 'sri.n#test.in'),
(21, 'ORD0021', 5, 3, 'C', 'sri.n#test.in'),
(22, 'ORD0022', 13, 3, 'C', 'sri.n#test.in'),
(23, 'ORD0023', 13, 3, 'C', 'sri.n#test.in'),
(24, 'ORD0024', 13, 3, 'C', 'sri.n#test.in'),
(25, 'ORD0025', 13, 3, 'C', 'sri.n#test.in'),
(26, 'ORD0026', 13, 3, 'C', 'sri.n#test.in'),
(27, 'ORD0027', 13, 3, 'C', 'sri.n#test.in'),
(28, 'ORD0028', 13, 3, 'C', 'sri.n#test.in'),
(29, 'ORD0029', 13, 3, 'C', 'sri.n#test.in'),
(30, 'ORD0030', 13, 3, 'C', 'sri.n#test.in'),
(31, 'ORD0031', 13, 3, 'C', 'sri.n#test.in'),
(32, 'ORD0032', 13, 3, 'C', 'sri.n#test.in'),
(33, 'ORD0033', 13, 3, 'C', 'sri.n#test.in'),
(34, 'ORD0034', 13, 3, 'C', 'sri.n#test.in'),
(35, 'ORD0035', 13, 3, 'C', 'sri.n#test.in'),
(36, 'ORD0036', 13, 3, 'C', 'sri.n#test.in'),
(37, 'ORD0037', 13, 3, 'C', 'sri.n#test.in'),
(38, 'ORD0038', 13, 3, 'C', 'sri.n#test.in'),
(39, 'ORD0039', 19, 3, 'C', 'sri.n#test.in'),
(40, 'ORD0040', 13, 3, 'C', 'sri.n#test.in'),
(41, 'ORD0041', 13, 3, 'C', 'sri.n#test.in'),
(42, 'ORD0042', 13, 3, 'C', 'sri.n#test.in'),
(43, 'ORD0043', 13, 3, 'C', 'sri.n#test.in'),
(44, 'ORD0044', 13, 3, 'C', 'sri.n#test.in'),
(45, 'ORD0045', 13, 3, 'C', 'sri.n#test.in'),
(46, 'ORD0046', 13, 3, 'C', 'sri.n#test.in'),
(47, 'ORD0047', 13, 3, 'C', 'sri.n#test.in'),
(48, 'ORD0048', 13, 3, 'C', 'sri.n#test.in'),
(49, 'ORD0049', 19, 3, 'C', 'sri.n#test.in'),
(51, 'ORD0051', 13, 3, 'C', 'sri.n#test.in'),
(52, 'ORD0052', 13, 3, 'C', 'sri.n#test.in'),
(53, 'ORD0053', 13, 3, 'C', 'sri.n#test.in'),
(54, 'ORD0054', 13, 10, 'G', 'sri.nas#test.in'),
(55, 'ORD0055', 13, 11, 'G', 'sri.nasqw#test.in'),
(56, 'ORD0056', 13, 12, 'G', 'sri.nqw#test.in'),
(57, 'ORD0057', 13, 13, 'G', 'sri.nas1123#test.in'),
(58, 'ORD0058', 13, 14, 'G', 'sri.nqw13#test.in'),
(59, 'ORD0059', 13, 15, 'G', 'sri.nas123#test.in'),
(60, 'ORD0060', 13, 16, 'G', 'sri.nas12345#test.in'),
(61, 'ORD0061', 13, 17, 'G', 'sri.nqw123#test.in'),
(62, 'ORD0062', 13, 18, 'G', 'sri.nas123111#test.in'),
(63, 'ORD0063', 13, 19, 'G', 'sri#test.in'),
(64, 'ORD0064', 13, 20, 'G', 'sri.nas111#test.in'),
(65, 'ORD0065', 13, 21, 'G', 'sri.nas12354klk#test.in'),
(66, 'ORD0066', 13, 22, 'G', 'sri.nas123879#test.in'),
(67, 'ORD0067', 13, 23, 'G', 'sri.nasasd#test.in'),
(68, 'ORD0068', 13, 24, 'G', 'sri.nasqwe#test.in'),
(69, 'ORD0069', 13, 25, 'G', 'sri.nas121212#test.in'),
(70, 'ORD0070', 13, 26, 'G', 'sri.nasqwqw#test.in'),
(71, 'ORD0071', 13, 27, 'G', 'sri.nqw321#test.in'),
(72, 'ORD0072', 13, 28, 'G', 'sri.nas123123#test.in'),
(73, 'ORD0073', 13, 3, 'C', 'sri.n#test.in'),
(74, 'ORD0074', 13, 3, 'C', 'sri.n#test.in'),
(75, 'ORD0075', 13, 3, 'C', 'sri.n#test.in'),
(76, 'ORD0076', 13, 3, 'C', 'sri.n#test.in'),
(77, 'ORD0077', 13, 3, 'C', 'sri.n#test.in'),
(78, 'ORD0078', 13, 3, 'C', 'sri.n#test.in'),
(79, 'ORD0079', 13, 3, 'C', 'sri.n#test.in'),
(121, 'ORD0121', 13, 52, 'G', 'sssri.n123123#test.in'),
(122, 'ORD0122', 13, 3, 'C', 'sri.n#test.in'),
(123, 'ORD0123', 13, 3, 'C', 'sri.n#test.in'),
(84, 'ORD0084', 13, 3, 'C', 'sri.n#test.in'),
(86, 'ORD0086', 13, 3, 'C', 'sri.n#test.in'),
(87, 'ORD0087', 13, 3, 'C', 'sri.n#test.in'),
(89, 'ORD0089', 13, 31, 'G', 'royalrenga#test.in'),
(90, 'ORD0090', 13, 32, 'G', 'nsri.n#test.in'),
(91, 'ORD0091', 13, 33, 'G', 'nnsri.n#test.in'),
(92, 'ORD0092', 13, 3, 'C', 'sri.n#test.in'),
(93, 'ORD0093', 13, 34, 'G', 'sssri.n#test.in'),
(94, 'ORD0094', 13, 35, 'G', 'qwsri.n#test.in'),
(95, 'ORD0095', 13, 36, 'G', 'snsri.n#test.in'),
(96, 'ORD0096', 13, 37, 'G', 'ncnsri.n#test.in'),
(97, 'ORD0097', 13, 38, 'G', 'nwnsri.n#test.in'),
(98, 'ORD0098', 13, 39, 'G', 'ncnasri.n#test.in'),
(99, 'ORD0099', 13, 40, 'G', 'ncnsasri.n#test.in'),
(100, 'ORD0100', 13, 41, 'G', 'ncqqnasri.n#test.in'),
(101, 'ORD0101', 13, 42, 'G', 'asdqazsri.nas123#test.in'),
(102, 'ORD0102', 13, 43, 'G', 'nacqqnasri.n#test.in'),
(103, 'ORD0103', 13, 3, 'C', 'sri.n#test.in'),
(104, 'ORD0104', 13, 3, 'C', 'sri.n#test.in'),
(105, 'ORD0105', 13, 3, 'C', 'sri.n#test.in'),
(106, 'ORD0106', 13, 3, 'C', 'sri.n#test.in'),
(107, 'ORD0107', 13, 3, 'C', 'sri.n#test.in'),
(108, 'ORD0108', 13, 0, 'G', 'sri.n#test.in'),
(109, 'ORD0109', 13, 3, 'C', 'sri.n#test.in'),
(110, 'ORD0110', 13, 3, 'C', 'sri.n#test.in'),
(111, 'ORD0111', 13, 44, 'G', 'qsw#test.in'),
(112, 'ORD0112', 13, 45, 'G', 'asdasd#test.in'),
(113, 'ORD0113', 13, 46, 'G', 'qweee#test.in'),
(114, 'ORD0114', 13, 47, 'G', 'qweqwe#test.in'),
(115, 'ORD0115', 13, 48, 'G', 'nsv123sri.n#test.in'),
(116, 'ORD0116', 13, 49, 'G', 'asdasdasd#test.in'),
(117, 'ORD0117', 13, 50, 'G', 'asdasdasdasd#test.in'),
(118, 'ORD0118', 13, 51, 'G', 'qwerew#test.in'),
(119, 'ORD0119', 13, 7, 'C', 'kvinocse86#test.in'),
(120, 'ORD0120', 13, 3, 'C', 'sri.n#test.in'),
(124, 'ORD0124', 13, 53, 'C', 'sri.n#test.in'),
(125, 'ORD0125', 13, 0, 'G', 'sri.n#test.in'),
(126, 'ORD0126', 13, 53, 'C', 'sri.n#test.in'),
(127, 'ORD0127', 13, 53, 'C', 'sri.n#test.in'),
(128, 'ORD0128', 13, 53, 'C', 'sri.n#test.in'),
(129, 'ORD0129', 13, 53, 'C', 'sri.n#test.in'),
(130, 'ORD0130', 13, 53, 'C', 'sri.n#test.in'),
(131, 'ORD0131', 13, 53, 'C', 'sri.n#test.in'),
(132, 'ORD0132', 13, 53, 'C', 'sri.n#test.in'),
(133, 'ORD0133', 13, 53, 'C', 'sri.n#test.in'),
(134, 'ORD0134', 13, 53, 'C', 'sri.n#test.in'),
(135, 'ORD0135', 13, 53, 'C', 'sri.n#test.in'),
(136, 'ORD0136', 13, 53, 'C', 'sri.n#test.in'),
(137, 'ORD0137', 13, 53, 'C', 'sri.n#test.in'),
(138, 'ORD0138', 13, 53, 'C', 'sri.n#test.in'),
(139, 'ORD0139', 13, 0, 'G', 'sri.n321#test.in'),
(140, 'ORD0140', 13, 53, 'C', 'sri.n123#test.in'),
(141, 'ORD0141', 13, 53, 'C', 'sri.n#test.in'),
(142, 'ORD0142', 13, 53, 'C', 'sri.n#test.in'),
(143, 'ORD0143', 13, 55, 'G', 'vesri#test.in'),
(144, 'ORD0144', 13, 56, 'G', 'vesri.n#test.in'),
(145, 'ORD0145', 13, 57, 'G', 'veesri.n#test.in'),
(146, 'ORD0146', 13, 58, 'G', 'veeisri.n#test.in'),
(147, 'ORD0147', 13, 59, 'G', 'ren#test.in'),
(148, 'ORD0148', 13, 60, 'G', 'ren1#test.in'),
(149, 'ORD0149', 13, 53, 'C', 'sri.n#test.in'),
(150, 'ORD0150', 13, 53, 'C', 'sri.n#test.in'),
(151, 'ORD0151', 13, 53, 'C', 'sri.n#test.in');
CREATE TABLE IF NOT EXISTS `my_restaurant` (
`restaurant_id` int(11) NOT NULL AUTO_INCREMENT,
`restaurant_name` varchar(100) NOT NULL,
PRIMARY KEY (`restaurant_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ;
INSERT INTO `my_restaurant` (`restaurant_id`, `restaurant_name`) VALUES
(1, 'Fuji Sushi'),
(2, 'Big Pete''s Pizza'),
(3, 'Vinos Pizza'),
(4, 'Wafaa and Mikes Cafe'),
(5, 'Fuji Sushi (San Marco Blvd)'),
(6, 'Midtown Deli + Cafe'),
(7, 'De Real Ting Cafe'),
(8, 'Alex Delicatessen'),
(9, 'Two Doors Down Restaurant'),
(10, 'Ginas Deli'),
(11, 'The Mudville Grille (Beach Blvd)'),
(12, 'Casbah Cafe'),
(13, 'Alexander Grill'),
(14, 'The Southern Grill'),
(15, 'Cool Moose Cafe'),
(16, 'Basil: Thai and Sushi'),
(17, 'Hot Wok'),
(18, 'China Joy'),
(19, 'Blu Diner'),
(21, 'New Test restaurant'),
(22, 'testing res');
I have written the following SQL for Most Order done by customer with restauant.
SELECT o.customeremail AS custemail, o.restaurant_id, rest.restaurant_name, COUNT( o.customeremail ) AS totalordercount
FROM my_order AS o
INNER JOIN my_customer AS cust ON cust.customer_email = o.customeremail
INNER JOIN my_restaurant AS rest ON rest.restaurant_id = o.restaurant_id
WHERE o.orderid IS NOT NULL
GROUP BY o.restaurant_id
ORDER BY totalordercount DESC
I'm getting the result like this.
custemail restaurant_id restaurant_name totalordercount
sri.n#test.in 13 Alexander Grill 79
vinothini.k#test.in 3 Vinos Pizza 12
sri.n#test.in 2 Big Pete's Pizza 5
sri.n#test.in 19 Blu Diner 2
vinothini.k#test.in 1 Fuji Sushi 1
sri.n#test.in 8 Alex Delicatessen 1
sri.n#test.in 5 Fuji Sushi (San Marco Blvd) 1
SELECT *
FROM `my_order`
WHERE `restaurant_id` =13
AND `customeremail` = 'sri.n#test.in'
LIMIT 0 , 30
I'm getting from above query with 71 rows only. But my query is showing the rows 79
sri.n#test.in 13 Alexander Grill 79
But I need the output like this.
custemail restaurant_id restaurant_name totalordercount
sri.n#test.in 13 Alexander Grill 71
vinothini.k#test.in 3 Vinos Pizza 12
sri.n#test.in 2 Big Pete's Pizza 5
sri.n#test.in 19 Blu Diner 2
vinothini.k#test.in 1 Fuji Sushi 1
sri.n#test.in 8 Alex Delicatessen 1
sri.n#test.in 5 Fuji Sushi (San Marco Blvd) 1
Thanks in advance
First, you're misusing the pernicious nonstandard MySQL extension to GROUP BY by trying to display a customer email in your result set aggregated by restaurant. Your misuse of this causes your result set to contain arbitrary values of custemail.
Second, this simple query reveals that there are 121 orders for the Alexander Grill. Your claim that there are only 71 seems to be incorrect.
SELECT COUNT(*), o.restaurant_id, r.restaurant_name
FROM my_order AS o
LEFT JOIN my_restaurant AS r ON o.restaurant_id = r.restaurant_id
GROUP BY o.restaurant_id, r.restaurant_name
Third, there are quite a few orders with a customer_id that isn't found in your customer table. You're using INNER JOIN so your query is dropping and not counting those orders. LEFT JOIN will restore those dropped records. But still, you are not using the results of that JOIN.
Fourth, your WHERE o.orderid IS NOT NULL clause is pointless: that column is a primary key.
Try this query leaving out the my_customer table JOIN:
SELECT o.restaurant_id,
rest.restaurant_name,
COUNT( DISTINCT o.customer_id ) AS distinctcustomercount,
COUNT( * ) AS totalordercount
FROM my_order AS o
LEFT JOIN my_restaurant AS rest ON rest.restaurant_id = o.restaurant_id
GROUP BY o.restaurant_id, rest.restaurant_name /* fix GROUP BY misuse */
ORDER BY COUNT( * ) DESC, rest.restaurant_name
I think this does the trick for you. It fully specifies the GROUP BY, and it does some kind of ordering among the restaurant rows that only have one order.
I think you are looking for visit count for restaurants for a customer email that has presence in the customer table. The other numbers just turn out to be right because the others from the 71'er have only visited one restaraunt.
You need to add the customer email to the group by if you want customers for each restaurant.
SELECT o.customeremail AS custemail,
o.restaurant_id, rest.restaurant_name, COUNT(o.customeremail ) AS totalordercount
FROM my_order AS o
INNER JOIN my_customer AS cust ON cust.customer_email = o.customeremail
INNER JOIN my_restaurant AS rest ON rest.restaurant_id = o.restaurant_id
GROUP BY o.restaurant_id, o.customeremail
ORDER BY totalordercount DESC
My answer is the same as Ollie's except I leave the customer join in, I think that presence is required to get the results expected.