随着越来越多的人这样做,我将使用短信api向某些人发送他们今天的约会提醒。
所以我使用此查询时没有任何问题地获取数据:
SELECT users.id as userid, consultations.patient_id as patientid, consultations.title as patientname, patients.mphone as phone
FROM users
JOIN consultations
ON users.id = consultations.user_id
JOIN patients
ON consultations.patient_id = patients.id
WHERE users.sms > 0 AND DATE(consultations.start) = (CURDATE() + INTERVAL 1 DAY) AND patients.mphone <> ''
与此同时,我想更新咨询表,并为每个consultations.reminder = true
输出的每一行设置substract 1 from users.sms
和users.id
...我可以做吗?那个?
我的第一个问题是,如果我添加count()
,查询会自动分组(或者只显示最后一行,不确定原因)。
如果你有建议......:)
答案 0 :(得分:1)
我的MySQL有点生疏,但您可以将结果插入到临时表中,然后从那里更新其他表。除此之外,select语句不能修改数据。
尝试这样的事情;
CREATE TEMPORARY TABLE IF NOT EXISTS TempAppointments AS (
SELECT users.id as userid, consultations.patient_id as patientid, consultations.title as patientname, patients.mphone as phone
FROM users
JOIN consultations
ON users.id = consultations.user_id
JOIN patients
ON consultations.patient_id = patients.id
WHERE users.sms > 0 AND DATE(consultations.start) = (CURDATE() + INTERVAL 1 DAY) AND patients.mphone <> '')
UPDATE C
SET reminder = true
FROM consultations AS C INNER JOIN TempAppointments AS T ON C.user_id = T.user_id
UPDATE U
SET sms = (sms - 1)
FROM Users AS U INNER JOIN TempAppointments AS T ON U.user_id = T.user_id
请记住最后删除临时表;
DROP TEMPORARY TABLE TempAppointments