任何人都可以将此查询转换为drupal插入查询

时间:2019-10-16 07:23:57

标签: php mysql sql drupal insert

INSERT INTO ip_ranges (ip, TYPE)
SELECT * FROM (SELECT '12.13.14.15','blacklist') AS tmp
WHERE NOT EXISTS (
    SELECT ip FROM ip_ranges WHERE ip = '12.13.14.15'
) LIMIT 1;

3 个答案:

答案 0 :(得分:0)

取决于您的Drupal版本。这是针对Drupal 7的:

<?php
$query = db_query('INSERT INTO ip_ranges (ip, TYPE) SELECT * FROM (SELECT '12.13.14.15','blacklist') AS tmp WHERE NOT EXISTS (SELECT ip FROM ip_ranges WHERE ip = '12.13.14.15') LIMIT 1;');
?>

答案 1 :(得分:0)

@kwoxer的答案略有增强(注意表名的{}):

<?php
$query = db_query('INSERT INTO {ip_ranges} (ip, TYPE) SELECT * FROM (SELECT '12.13.14.15','blacklist') AS tmp WHERE NOT EXISTS (SELECT ip FROM {ip_ranges} WHERE ip = '12.13.14.15') LIMIT 1;');
?>

答案 2 :(得分:0)

如果ipPRIMARY KEYUNIQUE键,则只需

INSERT IGNORE INTO ip_ranges (ip, `type`)
    VALUES
    ('12.13.14.15','blacklist')

如果以上是正确的 ,并且您想插入或将类型更改为“黑名单”,请执行

INSERT INTO ip_ranges (ip, `type`)
    VALUES
    ('12.13.14.15','blacklist')
    ON DUPLICATE KEY UPDATE
        `type` = VALUES(`type`)

如果ip不是主键,那么我不理解您的架构。