我正在尝试仅在满足另一个表的条件时更新表。这些表没有我可以加入的任何内容。 (phppos_app_config只是键/值对)。下面的查询不起作用,因为我不允许对子查询进行计数。
这是我想要实现的(不起作用,因为在sql中不允许;但只是显示逻辑)
UPDATE phppos_giftcards SET giftcard_number = TRIM(LEADING ';' FROM giftcard_number)
WHERE
COUNT
(
SELECT value FROM phppos_app_config
WHERE `key` = 'disable_giftcard_detection' and `value` = '0'
) =1
答案 0 :(得分:1)
我没有测试mysql所以请告诉我它是否不起作用我将删除
UPDATE phppos_giftcards SET giftcard_number = TRIM(LEADING ';' FROM giftcard_number)
WHERE ( SELECT count(*) FROM phppos_app_config
WHERE `key` = 'disable_giftcard_detection' and `value` = '0'
) = 1
答案 1 :(得分:0)
在子查询中使用Exists子句将简化子查询,并且由于避免不必要的比较而会更好地执行(假设如果要在PHPOS_APP_CONFIG表中至少有一条具有指定键的记录,则需要更新,值)
UPDATE phppos_giftcards SET giftcard_number = TRIM(LEADING ';' FROM giftcard_number)
WHERE exists
( SELECT count(*) FROM phppos_app_config
WHERE `key` = 'disable_giftcard_detection' and `value` = '0'
)