WHERE NOT EXISTS 插入多个值的问题
我想将表单中的多条记录插入名为“user”的表中,并且从表单提交的“items”值在另一个表名称“order”中不存在。 “订单”表中只有 2 个字段,即 id 和 items。
这是我的代码:
INSERT INTO user (id, username, email, address, items)
SELECT '$username1', '$email1', '$address1', '$items1'
UNION ALL
SELECT '$username2', '$email2', '$address2', '$items2'
UNION ALL
SELECT '$username3', '$email3', '$address3', '$items3'
FROM DUAL
WHERE NOT EXISTS(SELECT item FROM order)
假设“items”表包含 2 个设置数据:
id items
1 table01
2 chair01
因此,当我插入数据时:
john, [email protected], 12, Street, nail01
jennifer, [email protected], 13, Street, table01
peter, [email protected], 14, Street, spoon01
(defintely these data are keyin in the form)
假设第二条记录不会插入到“user”表中,因为“items”表中已经包含该项目。 但现在的结果是第一个和第二个数据将插入到“用户”表中,而第三个数据则不会。我用其他代码尝试了很多次,但仍然没有成功。
谁能给我一个建议吗? 谢谢。
I want to insert multiple records from the form into table named 'user' with the 'items' value submitted from the form do not exist in another table name 'order'. There is only 2 field in the 'order' table that is id and items.
Here is my code:
INSERT INTO user (id, username, email, address, items)
SELECT '$username1', '$email1', '$address1', '$items1'
UNION ALL
SELECT '$username2', '$email2', '$address2', '$items2'
UNION ALL
SELECT '$username3', '$email3', '$address3', '$items3'
FROM DUAL
WHERE NOT EXISTS(SELECT item FROM order)
Lets say 'items' table contain 2 set it data:
id items
1 table01
2 chair01
So when I insert data with:
john, [email protected], 12, Street, nail01
jennifer, [email protected], 13, Street, table01
peter, [email protected], 14, Street, spoon01
(defintely these data are keyin in the form)
Supposely the second record will not insert into 'user' table since in 'items' table already contain this item.
But now the result is the 1st and 2nd data will insert into 'user' table while 3rd data is not. I try with many times with other code but is still not success.
Can anyone give me a suggestion?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对你想要做的事情有点困惑,如果我错了,请纠正我,但这就是我从你的问题中了解到的:
你想从表单将数据添加到用户表中,但你只想添加用户其中订购了商品表中尚不存在的商品?
------ 现在证实了 ------
好吧,这就是我要解决的问题。
因此您正在使用 POST 方法从表单收集数据。
$_POST['电子邮件'] 等等。你想确保收集的数据是 100% 干净的,所以我使用我创建的一个方便的小 cleanString 函数...
并且会像这样使用它...
等等...
与你现在想要检查的所有干净数据如果订单表中存在该商品。因此,请按照以下方式进行查询:
然后检查是否找到结果,
如果您一次性执行多次插入,则可以将所有内容包装在 while 或 foreach 循环中以一次性处理所有内容。虽然考虑到它来自一个表单,但我猜你一次只会有 1 个输入?
希望这有帮助。
-------- 添加 while 循环 ------
所以你想一次性添加多条记录,这完全取决于你如何从表单中收集数据,为了给出准确的答案,我将需要更多有关如何从表单收集数据的信息。您是否每个字段都有 3 个字段来一次添加 3 个人,还是将所有数据保存到数组中以便稍后处理?
如果您将所有内容放入数组中,则这些内容将起作用。
这将一一获取每个用户,获取该用户的所有数据,检查该项目是否存在,如果不存在则进行处理。
您也可以使用 while 循环来完成此操作,但是在不知道如何获取数据的情况下,这种方式很难解释
I am a little confused with what you are trying to do, correct me if I'm wrong but this is what I understand from your question:
You want to add data into a Users table from a form, but you only want to add user which have order an item that does not already exist in the items table?
------ This now confirmed ------
Right well this is how I would go about it.
so you are collecting the data from the form using the POST method.
$_POST['email'] and so on. You want to make sure that the data collected is 100% clean so I use a handly little cleanString function I created...
and would use it like this...
etc...
with all of your clean data you now want to check if the item exists from the order table. so do a query along these lines:
Then check if a result was found
if you are doing multiple inserts in one go you can wrap this all in a while or foreach loop to process it all in one go. although considering it coming from a form I guess you will only have 1 input at a time?
hope this helps.
------- adding a while loop ------
so you are wanting to add multiple records in one go, this entirely depends on how you are collecting the data from the form, to give an exact answer I will need a bit more information about how you are collecting the data from the form. Do you have 3 of each field to add 3 people at once, or are you saving all the data to an array for processing later?
something along these lines will work if you are putting everything into an array
This will take each user one by one, get all the data for that user, check if the item exists and then process if it does not exist.
You can also do it with a while loop, but this way is hard to explain without knowing how you are acquiring the data
这看起来像 Oracle 语法...是吗?
您需要插入 id,但不要这样做。如果它由序列和触发器填充,但不需要插入它,那就没问题。
可能您的意思是
如果您的表“订单”在“项目”列上定义了唯一键,那么是的,当您尝试插入新项目时,插入将失败。更好的做法是首先查询订单表以检查该项目是否已存在,如果不存在,则创建它。
就像 AdriftUniform 所说,需要更清晰的信息!
This seems like Oracle syntax... Is it?
You require id to be inserted but do not do this. That is okay if it is populated by a sequence and trigger, but don't require it to be inserted.
Probably you meant
If your table 'order' has a Unique key defined on the 'items' column, then yes, your insert will fail when you try to insert a new item. A better practice would be to first query your order-table to check if this item already exists, and if not, create it.
Like AdriftUniform says, clearer info is needed!