简体   繁体   中英

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 ie

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM