1 回答

TA貢獻1770條經驗 獲得超3個贊
你想要or,而不是and- 否則你會搜索CardIndex同時具有所有三個值的行,這顯然永遠不會發生:
DELETE FROM CardData
WHERE
UserIndex = @UserIndex
AND (CardIndex = 1 OR CardIndex = 2 OR CardIndex = 3)
這可以縮短為IN:
DELETE FROM CardData
WHERE UserIndex = @UserIndex AND CardIndex IN (1, 2, 3)
SELECT請注意,在刪除值之前沒有意義。您可以直接觸發DELETE:如果沒有行符合條件,則不會發生實際刪除。
最后:不要在查詢字符串中連接變量;這是低效的,并且會將您的代碼暴露給 SQL 注入。相反,您應該使用參數化查詢(有大量在線資源可以解釋如何做到這一點)。
編輯
cardIndex僅當給定的所有三個值都可用時,您才想刪除所有三個記錄userIndex。假設沒有重復項(userIndex, cardIndex),一種方法是可更新的 CTE:
with cte as (
select count(*) over() cnt
from cardData
where userIndex = @UserIndex and cardIndex in (1, 2, 3)
)
delete from cte where cnt = 3
- 1 回答
- 0 關注
- 208 瀏覽
添加回答
舉報