我有以下示例表。mysql内部连接与3表问题与订单计数
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, '[email protected]'),
(5, '[email protected]'),
(6, '[email protected]'),
(8, '[email protected]'),
(63, '[email protected]'),
(56, '[email protected]'),
(57, '[email protected]'),
(58, '[email protected]'),
(59, '[email protected]'),
(60, '[email protected]'),
(61, '[email protected]'),
(62, '[email protected]'),
(53, '[email protected]'),
(54, '[email protected]'),
(55, '[email protected]');
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', '[email protected]'),
(2, 'ORD0002', 1, 6, 'C', '[email protected]'),
(3, 'ORD0003', 3, 6, 'C', '[email protected]'),
(4, 'ORD0004', 3, 6, 'C', '[email protected]'),
(5, 'ORD0005', 3, 0, 'G', '[email protected]'),
(6, 'ORD0006', 3, 6, 'C', '[email protected]'),
(7, 'ORD0007', 3, 6, 'C', '[email protected]'),
(8, 'ORD0008', 3, 6, 'C', '[email protected]'),
(9, 'ORD0009', 3, 6, 'C', '[email protected]'),
(10, 'ORD0010', 3, 6, 'C', '[email protected]'),
(11, 'ORD0011', 3, 6, 'C', '[email protected]'),
(12, 'ORD0012', 3, 6, 'C', '[email protected]'),
(13, 'ORD0013', 3, 6, 'C', '[email protected]'),
(14, 'ORD0014', 3, 6, 'C', '[email protected]'),
(15, 'ORD0015', 2, 3, 'C', '[email protected]'),
(16, 'ORD0016', 2, 3, 'C', '[email protected]'),
(17, 'ORD0017', 2, 3, 'C', '[email protected]'),
(18, 'ORD0018', 2, 3, 'C', '[email protected]'),
(19, 'ORD0019', 2, 3, 'C', '[email protected]'),
(20, 'ORD0020', 8, 3, 'C', '[email protected]'),
(21, 'ORD0021', 5, 3, 'C', '[email protected]'),
(22, 'ORD0022', 13, 3, 'C', '[email protected]'),
(23, 'ORD0023', 13, 3, 'C', '[email protected]'),
(24, 'ORD0024', 13, 3, 'C', '[email protected]'),
(25, 'ORD0025', 13, 3, 'C', '[email protected]'),
(26, 'ORD0026', 13, 3, 'C', '[email protected]'),
(27, 'ORD0027', 13, 3, 'C', '[email protected]'),
(28, 'ORD0028', 13, 3, 'C', '[email protected]'),
(29, 'ORD0029', 13, 3, 'C', '[email protected]'),
(30, 'ORD0030', 13, 3, 'C', '[email protected]'),
(31, 'ORD0031', 13, 3, 'C', '[email protected]'),
(32, 'ORD0032', 13, 3, 'C', '[email protected]'),
(33, 'ORD0033', 13, 3, 'C', '[email protected]'),
(34, 'ORD0034', 13, 3, 'C', '[email protected]'),
(35, 'ORD0035', 13, 3, 'C', '[email protected]'),
(36, 'ORD0036', 13, 3, 'C', '[email protected]'),
(37, 'ORD0037', 13, 3, 'C', '[email protected]'),
(38, 'ORD0038', 13, 3, 'C', '[email protected]'),
(39, 'ORD0039', 19, 3, 'C', '[email protected]'),
(40, 'ORD0040', 13, 3, 'C', '[email protected]'),
(41, 'ORD0041', 13, 3, 'C', '[email protected]'),
(42, 'ORD0042', 13, 3, 'C', '[email protected]'),
(43, 'ORD0043', 13, 3, 'C', '[email protected]'),
(44, 'ORD0044', 13, 3, 'C', '[email protected]'),
(45, 'ORD0045', 13, 3, 'C', '[email protected]'),
(46, 'ORD0046', 13, 3, 'C', '[email protected]'),
(47, 'ORD0047', 13, 3, 'C', '[email protected]'),
(48, 'ORD0048', 13, 3, 'C', '[email protected]'),
(49, 'ORD0049', 19, 3, 'C', '[email protected]'),
(51, 'ORD0051', 13, 3, 'C', '[email protected]'),
(52, 'ORD0052', 13, 3, 'C', '[email protected]'),
(53, 'ORD0053', 13, 3, 'C', '[email protected]'),
(54, 'ORD0054', 13, 10, 'G', '[email protected]'),
(55, 'ORD0055', 13, 11, 'G', '[email protected]'),
(56, 'ORD0056', 13, 12, 'G', '[email protected]'),
(57, 'ORD0057', 13, 13, 'G', '[email protected]'),
(58, 'ORD0058', 13, 14, 'G', '[email protected]'),
(59, 'ORD0059', 13, 15, 'G', '[email protected]'),
(60, 'ORD0060', 13, 16, 'G', '[email protected]'),
(61, 'ORD0061', 13, 17, 'G', '[email protected]'),
(62, 'ORD0062', 13, 18, 'G', '[email protected]'),
(63, 'ORD0063', 13, 19, 'G', '[email protected]'),
(64, 'ORD0064', 13, 20, 'G', '[email protected]'),
(65, 'ORD0065', 13, 21, 'G', '[email protected]'),
(66, 'ORD0066', 13, 22, 'G', '[email protected]'),
(67, 'ORD0067', 13, 23, 'G', '[email protected]'),
(68, 'ORD0068', 13, 24, 'G', '[email protected]'),
(69, 'ORD0069', 13, 25, 'G', '[email protected]'),
(70, 'ORD0070', 13, 26, 'G', '[email protected]'),
(71, 'ORD0071', 13, 27, 'G', '[email protected]'),
(72, 'ORD0072', 13, 28, 'G', '[email protected]'),
(73, 'ORD0073', 13, 3, 'C', '[email protected]'),
(74, 'ORD0074', 13, 3, 'C', '[email protected]'),
(75, 'ORD0075', 13, 3, 'C', '[email protected]'),
(76, 'ORD0076', 13, 3, 'C', '[email protected]'),
(77, 'ORD0077', 13, 3, 'C', '[email protected]'),
(78, 'ORD0078', 13, 3, 'C', '[email protected]'),
(79, 'ORD0079', 13, 3, 'C', '[email protected]'),
(121, 'ORD0121', 13, 52, 'G', '[email protected]'),
(122, 'ORD0122', 13, 3, 'C', '[email protected]'),
(123, 'ORD0123', 13, 3, 'C', '[email protected]'),
(84, 'ORD0084', 13, 3, 'C', '[email protected]'),
(86, 'ORD0086', 13, 3, 'C', '[email protected]'),
(87, 'ORD0087', 13, 3, 'C', '[email protected]'),
(89, 'ORD0089', 13, 31, 'G', '[email protected]'),
(90, 'ORD0090', 13, 32, 'G', '[email protected]'),
(91, 'ORD0091', 13, 33, 'G', '[email protected]'),
(92, 'ORD0092', 13, 3, 'C', '[email protected]'),
(93, 'ORD0093', 13, 34, 'G', '[email protected]'),
(94, 'ORD0094', 13, 35, 'G', '[email protected]'),
(95, 'ORD0095', 13, 36, 'G', '[email protected]'),
(96, 'ORD0096', 13, 37, 'G', '[email protected]'),
(97, 'ORD0097', 13, 38, 'G', '[email protected]'),
(98, 'ORD0098', 13, 39, 'G', '[email protected]'),
(99, 'ORD0099', 13, 40, 'G', '[email protected]'),
(100, 'ORD0100', 13, 41, 'G', '[email protected]'),
(101, 'ORD0101', 13, 42, 'G', '[email protected]'),
(102, 'ORD0102', 13, 43, 'G', '[email protected]'),
(103, 'ORD0103', 13, 3, 'C', '[email protected]'),
(104, 'ORD0104', 13, 3, 'C', '[email protected]'),
(105, 'ORD0105', 13, 3, 'C', '[email protected]'),
(106, 'ORD0106', 13, 3, 'C', '[email protected]'),
(107, 'ORD0107', 13, 3, 'C', '[email protected]'),
(108, 'ORD0108', 13, 0, 'G', '[email protected]'),
(109, 'ORD0109', 13, 3, 'C', '[email protected]'),
(110, 'ORD0110', 13, 3, 'C', '[email protected]'),
(111, 'ORD0111', 13, 44, 'G', '[email protected]'),
(112, 'ORD0112', 13, 45, 'G', '[email protected]'),
(113, 'ORD0113', 13, 46, 'G', '[email protected]'),
(114, 'ORD0114', 13, 47, 'G', '[email protected]'),
(115, 'ORD0115', 13, 48, 'G', '[email protected]'),
(116, 'ORD0116', 13, 49, 'G', '[email protected]'),
(117, 'ORD0117', 13, 50, 'G', '[email protected]'),
(118, 'ORD0118', 13, 51, 'G', '[email protected]'),
(119, 'ORD0119', 13, 7, 'C', '[email protected]'),
(120, 'ORD0120', 13, 3, 'C', '[email protected]'),
(124, 'ORD0124', 13, 53, 'C', '[email protected]'),
(125, 'ORD0125', 13, 0, 'G', '[email protected]'),
(126, 'ORD0126', 13, 53, 'C', '[email protected]'),
(127, 'ORD0127', 13, 53, 'C', '[email protected]'),
(128, 'ORD0128', 13, 53, 'C', '[email protected]'),
(129, 'ORD0129', 13, 53, 'C', '[email protected]'),
(130, 'ORD0130', 13, 53, 'C', '[email protected]'),
(131, 'ORD0131', 13, 53, 'C', '[email protected]'),
(132, 'ORD0132', 13, 53, 'C', '[email protected]'),
(133, 'ORD0133', 13, 53, 'C', '[email protected]'),
(134, 'ORD0134', 13, 53, 'C', '[email protected]'),
(135, 'ORD0135', 13, 53, 'C', '[email protected]'),
(136, 'ORD0136', 13, 53, 'C', '[email protected]'),
(137, 'ORD0137', 13, 53, 'C', '[email protected]'),
(138, 'ORD0138', 13, 53, 'C', '[email protected]'),
(139, 'ORD0139', 13, 0, 'G', '[email protected]'),
(140, 'ORD0140', 13, 53, 'C', '[email protected]'),
(141, 'ORD0141', 13, 53, 'C', '[email protected]'),
(142, 'ORD0142', 13, 53, 'C', '[email protected]'),
(143, 'ORD0143', 13, 55, 'G', '[email protected]'),
(144, 'ORD0144', 13, 56, 'G', '[email protected]'),
(145, 'ORD0145', 13, 57, 'G', '[email protected]'),
(146, 'ORD0146', 13, 58, 'G', '[email protected]'),
(147, 'ORD0147', 13, 59, 'G', '[email protected]'),
(148, 'ORD0148', 13, 60, 'G', '[email protected]'),
(149, 'ORD0149', 13, 53, 'C', '[email protected]'),
(150, 'ORD0150', 13, 53, 'C', '[email protected]'),
(151, 'ORD0151', 13, 53, 'C', '[email protected]');
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');
我已经为顾客用restauant完成的大多数订单编写了以下SQL。
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
我得到这样的结果。
custemail restaurant_id restaurant_name totalordercount
[email protected] 13 Alexander Grill 79
[email protected] 3 Vinos Pizza 12
[email protected] 2 Big Pete's Pizza 5
[email protected] 19 Blu Diner 2
[email protected] 1 Fuji Sushi 1
[email protected] 8 Alex Delicatessen 1
[email protected] 5 Fuji Sushi (San Marco Blvd) 1
SELECT *
FROM `my_order`
WHERE `restaurant_id` =13
AND `customeremail` = '[email protected]'
LIMIT 0 , 30
我从上面查询只有71行。但我的查询显示行79
[email protected] 13 Alexander Grill 79
但我需要这样的输出。
custemail restaurant_id restaurant_name totalordercount
[email protected] 13 Alexander Grill 71
[email protected] 3 Vinos Pizza 12
[email protected] 2 Big Pete's Pizza 5
[email protected] 19 Blu Diner 2
[email protected] 1 Fuji Sushi 1
[email protected] 8 Alex Delicatessen 1
[email protected] 5 Fuji Sushi (San Marco Blvd) 1
在此先感谢