0

我收到以下查询:

INSERT INTO contracts_settings (contract_id, setting_id, setting_value)
VALUES (:contract_id, (
    SELECT  setting_id
    FROM    settings
    WHERE   setting_type = :setting_type
        AND setting_name = :setting_name
    LIMIT   1
), :setting_value)
ON DUPLICATE KEY UPDATE setting_value = :setting_value

带有前缀的值:使用 PHP 替换为数据PDO::bindBalue

如果内部查询什么也没找到(它返回 NULL),还要插入一个 NULL 语句。如何避免这种情况?

谢谢。

4

1 回答 1

2

INSERT ... VALUES语法转换为INSERT ... SELECT

INSERT INTO contracts_settings 
  (contract_id, setting_id, setting_value)
SELECT 
    :contract_id,
    setting_id,
    :setting_value
FROM  settings
WHERE setting_type = :setting_type
  AND setting_name = :setting_name
LIMIT   1

ON DUPLICATE KEY UPDATE 
    setting_value = :setting_value ;
于 2013-01-28T19:55:52.327 回答