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;
答案 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)
如果ip
是PRIMARY KEY
或UNIQUE
键,则只需
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
不是主键,那么我不理解您的架构。