MySQL 条件插入。适用于 phpMyAdmin 但不适用于 PHP 脚本

发布于 2024-10-08 21:35:10 字数 459 浏览 7 评论 0原文

我正在尝试从 PHP 脚本创建一个条件 INSERT 到我的 MySQL 数据库中。以下 SQL 语法适用于 phpMyAdmin,但不适用于我的 PHP 脚本:(

INSERT INTO profiles (id, firstname)
SELECT "22","John" from profiles
WHERE NOT EXISTS (
SELECT * FROM li_profiles
WHERE li_p_firstname = "John"
)

请注意,“id”是主键,“firstname”不是键或唯一)

可能是问题的一部分的奇怪之处是,当我运行当 phpMyAdmin 中的 SQL 确实“工作”(意味着添加了 id“22”和名字“John”的新记录)时,我收到以下警告:“#1062 - 键 1 的重复条目“22” ” 但表中没有 id 为 22 的先前条目。??!!

这是怎么回事?

I am trying to create a conditional INSERT into my MySQL databate from a PHP script. The following SQL syntax works in phpMyAdmin, but not in my PHP Script:

INSERT INTO profiles (id, firstname)
SELECT "22","John" from profiles
WHERE NOT EXISTS (
SELECT * FROM li_profiles
WHERE li_p_firstname = "John"
)

(Note that "id" is the primary key, "firstname" is not a key or unique)

Something weird that might be part of the issue is that when I run that SQL in phpMyAdmin, while it does "work" (meaning that a new record is added with the id "22" and the firstname "John") I get the following warning: "#1062 - Duplicate entry '22' for key 1"
But the table didn't have a previous entry with id of 22. ??!!

What's going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

百合的盛世恋 2024-10-15 21:35:10

将 SELECT 更改为 VALUES

INSERT INTO profiles (id, firstname) VALUES("22","John") FROM profiles WHERE NOT EXISTS ( SELECT * FROM li_profiles WHERE li_p_firstname = "John" )

此外,如果您使用自动增量值,则应指定下一个值。另外,如果它是整数,请给出整数 (22) 而不是字符串 ("22")

Change SELECT to VALUES

INSERT INTO profiles (id, firstname) VALUES("22","John") FROM profiles WHERE NOT EXISTS ( SELECT * FROM li_profiles WHERE li_p_firstname = "John" )

Also, if you are using auto-increment values, you should specify the next value. Also, if its an integer, give an integer (22) not a string ("22")

記憶穿過時間隧道 2024-10-15 21:35:10

您将获得 iD 的重复条目,因为您要为 profiles 表中的每一行插入一个新行;对于 profiles 表中的每一行,li_profiles 表中都没有 John。您可以尝试

INSERT INTO profiles (id, firstname)
    SELECT "22","John" from profiles
        WHERE NOT EXISTS (SELECT * FROM li_profiles
                              WHERE li_p_firstname = "John")
        LIMIT 1;

消除重复的问题(如果有效,抱歉,但我自己没有检查过)。

You'll get a duplicate entry for the iD because you are inserting a new row for each row in the profiles table; for every row in the profiles table there is no John in the li_profiles table. You might try

INSERT INTO profiles (id, firstname)
    SELECT "22","John" from profiles
        WHERE NOT EXISTS (SELECT * FROM li_profiles
                              WHERE li_p_firstname = "John")
        LIMIT 1;

which would eliminate the duplicate problem (if it works, sorry but I haven't checked this myself).

孤云独去闲 2024-10-15 21:35:10

我以不同的方式解决了这个问题。 (我被告知 HAVING 语句很慢,所以我不确定这是最好的方法......但它是我可以工作的唯一方法。)

INSERT INTO profiles (id,firstname)
SELECT 22,'John'
FROM li_profiles
WHERE firstname = 'John'
HAVING COUNT(*) = 0;

I figured it out in a different way. (I'm told that the HAVING statement is slow, so I'm not sure that it's the best way... but it the only method I've gotten to work.)

INSERT INTO profiles (id,firstname)
SELECT 22,'John'
FROM li_profiles
WHERE firstname = 'John'
HAVING COUNT(*) = 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文