PHP INSERT到表中

时间:2014-01-27 22:46:22

标签: php mysql sql

这是我的查询。我不知道为什么不运行它?我的语法错了吗?

$query = "INSERT IGNORE INTO ps_search_index (id_product, id_word, weight) 
SELECT DISTINCT Product_ID, Product_ID + 5000 'id_word', '1' 
FROM ps_product_part_number 
    WHERE   API != '' AND API IS NOT NULL"; 

2 个答案:

答案 0 :(得分:1)

这是一个为php工作的INSERT代码示例。

如果您想使用SQL代码添加5000,我强烈建议您在将其放入SQL代码之前在php上执行此操作。

<强> EDITED

$id_product += 5000;

$query = "INSERT INTO ps_search_index (id_product, id_word, weight) VALUES ('$id_product', '$id_word', '$weight')";

答案 1 :(得分:1)

可能你的意思是Product_ID + 5000 AS id_word

此外,我会删除忽略,因此您可以在开发时读取任何错误。

INSERT  INTO ps_search_index (id_product, id_word, weight) 
SELECT DISTINCT Product_ID, Product_ID + 5000 AS  id_word , '1' 
FROM ps_product_part_number 
    WHERE   API != '' AND API IS NOT NULL 

另外,我认为您不需要AS id_word,因为INSERT...SELECT语法会按照您声明的顺序在字段中插入值,因此您不需要使用别名,所以这将是是一样的:

INSERT  INTO ps_search_index (id_product, id_word, weight) 
SELECT DISTINCT Product_ID, Product_ID + 5000, '1' 
FROM ps_product_part_number 
    WHERE   API != '' AND API IS NOT NULL"