gpt4 book ai didi

mysql - 同时选择、连接和更新

转载 作者:行者123 更新时间:2023-11-29 11:33:33 26 4
gpt4 key购买 nike

随着越来越多的人这样做,我将使用短信 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从 users.sms 中减去 1对于每个users.id...我可以这样做吗?

我的第一个问题是,如果我添加 count(),查询会自动分组(或者只显示最后一行,不知道为什么)。

如果您有建议...:)

最佳答案

我的 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

关于mysql - 同时选择、连接和更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36997902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com