简体   繁体   English

使用联接更新表的多行列

[英]Update a column of multiple rows of a table using joins

EDIT 编辑

I tried this query but got an error: 我尝试了此查询,但出现错误:

UPDATE consultation_med
SET consultation_med.given_quantity = 0
FROM
consultation_med
left join consultation on consultation.consultation_id = consultation_med.consultation_id
left join visit on visit.visit_id = consultation.visit_id
 WHERE visit.visit_id='191'
           AND consultation_med.given_quantity = '361'

the error: 错误:

Error Code: 1064. You have an error in your SQL syntax; 错误代码:1064。 check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM consultation_med left join consultation on consultation.consultation_id = c' at line 3 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第3行的'FROM consulting_med left join consulting on Consultation.consultation_id = c'附近使用

END EDIT 结束编辑

I need to update a table called consultation_med and change the given_quantity of a medication into 0 when the status of a visit visit_status is changed from Active to Inactive . 我需要更新一个名为表consultation_med和改变given_quantity药物到0时访问的状态visit_status从改变ActiveInactive

Please take note that in the visit table, each visit_id can have multiple consultation_id in the next table which is consultation . 请大家注意,在访问表,每个visit_id可以有多个consultation_id在接下来的表,该表consultation And in each consultation_id doctors can give multiple medication with different quantities so we have now for each consultation_id multiple rows in consultation_med table. 而且,在每个consultation_id医生可以给予不同剂量的多种药物,因此我们现在在consultation_med表中为每个consultation_id多行。

Here are the tables: 表格如下:

在此处输入图片说明

So let's take the visit_id=173 that already had 4 consultation_id and in each consultation doctors prescribed 1 medication. 因此,让我们以已经具有4个Consultation_id的visit_id=173 ,在每次咨询中,医生都开了1种药物。 So we have 4 rows related to visit where visit_id=173 in consultation_med table. 因此,我们有4个能去相关行visit_id=173consultation_med表。 The data entry had an error and he need to send an request to the admin by changing the status of this visit to Inactive , so every medication quantity given should be back to inital storage, thus we should change the given_quantity of each 4 rows to 0 . 数据输入有错误,他需要通过将此访问的状态更改为Inactive来向管理员发送请求,因此,给定的每种药物数量应返回初始存储,因此我们应将每4行的given_quantity更改为0

I tried in PHP the following query but apparently it is taking just one row and updating it: 我在PHP中尝试了以下查询,但显然它只占用一行并更新了它:

$med_pharmacy_id = $res['med_pharmacy_id'];
$consultation_med_id = $res['consultation_med_id'];

$update = "UPDATE consultation_med 
SET given_quantity = 0 
WHERE med_pharmacy_id = :mid
AND clinic_id = :cid 
AND consultation_med_id = :cmid";

Then I tried to use left JOIN inside update query: 然后我尝试在更新查询中使用左JOIN:

UPDATE consultation_med 
left join consultation on consultation.consultation_id = consultation_med.consultation_id
left join visit on visit.visit_id = consultation.visit_id

SET consultation_med.given_quantity = 0 
WHERE visit.visit_id='173'
AND consultation_med.given_quantity = '361'

The query is still wrong plus an extra error which is: 查询还是错误的,外加一个额外的错误是:

Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect. 错误代码:1175。您正在使用安全更新模式,并且尝试更新不具有使用KEY列的WHERE的表。要禁用安全模式,请在“首选项”->“ SQL编辑器”中切换选项,然后重新连接。

您需要在SQL编辑器中的编辑或设置->首选项中将错误“ 1175”更改为DISABLE SAFE MODE,或者您应该使用phpmyadmin。

In MySQL, you should write the update statement as: update-join-set-where 在MySQL中,您应将update语句写为:update-join-set-where

Something like this: 像这样:

UPDATE consultation_med
LEFT JOIN consultation on consultation.consultation_id = consultation_med.consultation_id
LEFT JOIN visit on visit.visit_id = consultation.visit_id
SET consultation_med.given_quantity = 0
 WHERE visit.visit_id='191'
           AND consultation_med.given_quantity = '361'

For more details, visit this link 有关更多详细信息,请访问链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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