在 SQL 更新中使用 Distinct

发布于 2024-12-01 06:37:32 字数 561 浏览 1 评论 0原文

这是我需要转换为更新的选择:

SELECT DISTINCT f.SectionID, f.Name, v.Enabled
FROM SETTING_VALUE v
INNER JOIN SETTING s ON v.SettingID = s.SettingID
INNER JOIN LU_FIELD f ON f.FieldID = s.FieldID
WHERE v.DisplayValue LIKE '%Miami%'
AND f.ControlName LIKE '%City%'

我的尝试:

UPDATE SETTING_VALUE
SET Enabled = 0
FROM SETTING_VALUE v
INNER JOIN SETTING s ON v.SettingID = s.SettingID
INNER JOIN LU_FIELD f ON f.FieldID = s.FieldID
WHERE v.DisplayValue LIKE '%Miami%'
AND f.ControlName LIKE '%City%'

不确定如何应用不同

Here is the Select I need to convert to an Update:

SELECT DISTINCT f.SectionID, f.Name, v.Enabled
FROM SETTING_VALUE v
INNER JOIN SETTING s ON v.SettingID = s.SettingID
INNER JOIN LU_FIELD f ON f.FieldID = s.FieldID
WHERE v.DisplayValue LIKE '%Miami%'
AND f.ControlName LIKE '%City%'

My attempt:

UPDATE SETTING_VALUE
SET Enabled = 0
FROM SETTING_VALUE v
INNER JOIN SETTING s ON v.SettingID = s.SettingID
INNER JOIN LU_FIELD f ON f.FieldID = s.FieldID
WHERE v.DisplayValue LIKE '%Miami%'
AND f.ControlName LIKE '%City%'

Not sure how to apply Distinct

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月亮邮递员 2024-12-08 06:37:32

根据您的评论,所有匹配的行都应该更新,您不需要不同。只需执行此操作:

UPDATE SETTING_VALUE
SET Enabled = 0
FROM SETTING_VALUE v
INNER JOIN SETTING s ON v.SettingID = s.SettingID
INNER JOIN LU_FIELD f ON f.FieldID = s.FieldID
WHERE v.DisplayValue LIKE '%Miami%'
AND f.ControlName LIKE '%City%'

您在选择中看到的所有内容都将被更新。

Based on your comment, all of the matching rows should be updated you don't need distinct. Just execute this:

UPDATE SETTING_VALUE
SET Enabled = 0
FROM SETTING_VALUE v
INNER JOIN SETTING s ON v.SettingID = s.SettingID
INNER JOIN LU_FIELD f ON f.FieldID = s.FieldID
WHERE v.DisplayValue LIKE '%Miami%'
AND f.ControlName LIKE '%City%'

Everything that you saw in your select will be updated.

峩卟喜欢 2024-12-08 06:37:32

选项 1:使用临时表来保存初始查询结果,然后根据查询结果进行更新。

选项2:

Insert into SETTING_VALUE
(SectionID, Name, Enabled)
SELECT DISTINCT f.SectionID, f.Name, v.Enabled
FROM SETTING_VALUE v
INNER JOIN SETTING s ON v.SettingID = s.SettingID
INNER JOIN LU_FIELD f ON f.FieldID = s.FieldID
WHERE v.DisplayValue LIKE '%Miami%'
AND f.ControlName LIKE '%City%'

Option 1: Use a temporary table to hold your initial query results, then update based on the query results.

Option 2:

Insert into SETTING_VALUE
(SectionID, Name, Enabled)
SELECT DISTINCT f.SectionID, f.Name, v.Enabled
FROM SETTING_VALUE v
INNER JOIN SETTING s ON v.SettingID = s.SettingID
INNER JOIN LU_FIELD f ON f.FieldID = s.FieldID
WHERE v.DisplayValue LIKE '%Miami%'
AND f.ControlName LIKE '%City%'
沩ん囻菔务 2024-12-08 06:37:32
UPDATE SETTING_VALUE
   SET Enabled = 0
 WHERE DisplayValue LIKE '%Miami%'
       AND EXISTS (
                   SELECT *
                     FROM SETTING s 
                    WHERE SETTING_VALUE.SettingID = s.SettingID
                          AND EXISTS (
                                      SELECT *
                                        FROM LU_FIELD f 
                                       WHERE f.FieldID = s.FieldID
                                             AND f.ControlName LIKE '%City%'
                                     )
                  );
UPDATE SETTING_VALUE
   SET Enabled = 0
 WHERE DisplayValue LIKE '%Miami%'
       AND EXISTS (
                   SELECT *
                     FROM SETTING s 
                    WHERE SETTING_VALUE.SettingID = s.SettingID
                          AND EXISTS (
                                      SELECT *
                                        FROM LU_FIELD f 
                                       WHERE f.FieldID = s.FieldID
                                             AND f.ControlName LIKE '%City%'
                                     )
                  );
孤者何惧 2024-12-08 06:37:32

您可以尝试以下方法

UPDATE v
SET Enabled = 0 
FROM SETTING_VALUE v 
WHERE EXISTS(
   select * from SETTING s 
   INNER JOIN LU_FIELD f ON f.FieldID = s.FieldID 
   WHERE v.DisplayValue LIKE '%Miami%' 
   AND f.ControlName LIKE '%City%' 
   AND v.SettingID = s.SettingID
) 

You can try the following

UPDATE v
SET Enabled = 0 
FROM SETTING_VALUE v 
WHERE EXISTS(
   select * from SETTING s 
   INNER JOIN LU_FIELD f ON f.FieldID = s.FieldID 
   WHERE v.DisplayValue LIKE '%Miami%' 
   AND f.ControlName LIKE '%City%' 
   AND v.SettingID = s.SettingID
) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文