WHERE NOT EXISTS 插入多个值的问题

发布于 2024-12-06 15:33:59 字数 1257 浏览 2 评论 0原文

我想将表单中的多条记录插入名为“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 技术交流群。

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

发布评论

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

评论(2

勿挽旧人 2024-12-13 15:33:59

我对你想要做的事情有点困惑,如果我错了,请纠正我,但这就是我从你的问题中了解到的:

你想从表单将数据添加到用户表中,但你只想添加用户其中订购了商品表中尚不存在的商品?

------ 现在证实了 ------

好吧,这就是我要解决的问题。

因此您正在使用 POST 方法从表单收集数据。

$_POST['电子邮件'] 等等。你想确保收集的数据是 100% 干净的,所以我使用我创建的一个方便的小 cleanString 函数...

function cleanString($var)
{
    $var = strip_tags($var); //Removes all HTML tags
    $var = htmlentities($var); //Converts characters into HTML entities
    $var = stripslashes($var); //Removes any backslashes
    return mysql_real_escape_string($var); // Escapes all special characters
}

并且会像这样使用它...

$email = cleanString($_POST['email']);
$item = cleanString($_POST['item']);

等等...

与你现在想要检查的所有干净数据如果订单表中存在该商品。因此,请按照以下方式进行查询:

$query = "SELECT items FROM order WHERE items=".$item;
$result = mysql_query($query);

然后检查是否找到结果,

if(mysql_num_rows($result) > 0)
{
    // Result found do not process...
}
else
{
    // Result not found, process...
    $query = "INSERT INTO user ...";
}

如果您一次性执行多次插入,则可以将所有内容包装在 while 或 foreach 循环中以一次性处理所有内容。虽然考虑到它来自一个表单,但我猜你一次只会有 1 个输入?

希望这有帮助。

-------- 添加 while 循环 ------

所以你想一次性添加多条记录,这完全取决于你如何从表单中收集数据,为了给出准确的答案,我将需要更多有关如何从表单收集数据的信息。您是否每个字段都有 3 个字段来一次添加 3 个人,还是将所有数据保存到数组中以便稍后处理?

如果您将所有内容放入数组中,则这些内容将起作用。

foreach ($array as $user)
{
    $item = $user['item']
    etc...

    $query = "SELECT items FROM order WHERE items=".$item;
    $result = mysql_query($query);

    if(mysql_num_rows($result) > 0)
    {
        // Result found do not process...
    }
    else
    {
        // Result not found, process...
        $query = "INSERT INTO user ...";
    }
}

这将一一获取每个用户,获取该用户的所有数据,检查该项目是否存在,如果不存在则进行处理。

您也可以使用 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...

function cleanString($var)
{
    $var = strip_tags($var); //Removes all HTML tags
    $var = htmlentities($var); //Converts characters into HTML entities
    $var = stripslashes($var); //Removes any backslashes
    return mysql_real_escape_string($var); // Escapes all special characters
}

and would use it like this...

$email = cleanString($_POST['email']);
$item = cleanString($_POST['item']);

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:

$query = "SELECT items FROM order WHERE items=".$item;
$result = mysql_query($query);

Then check if a result was found

if(mysql_num_rows($result) > 0)
{
    // Result found do not process...
}
else
{
    // Result not found, process...
    $query = "INSERT INTO user ...";
}

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

foreach ($array as $user)
{
    $item = $user['item']
    etc...

    $query = "SELECT items FROM order WHERE items=".$item;
    $result = mysql_query($query);

    if(mysql_num_rows($result) > 0)
    {
        // Result found do not process...
    }
    else
    {
        // Result not found, process...
        $query = "INSERT INTO user ...";
    }
}

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

中二柚 2024-12-13 15:33:59

这看起来像 Oracle 语法...是吗?

INSERT INTO user (id, username, email, address, items)

您需要插入 id,但不要这样做。如果它由序列和触发器填充,但不需要插入它,那就没问题。

INSERT INTO user (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)
  • 您需要在每个联合部分中使用“FROM DUAL”
  • 您的 WHERE 子句仅影响第三个选择
  • 它也没有多大意义:“如果表顺序中没有行,则选择此行”。您需要指定您的排除项。我添加了一个联接(WHERE a.items = items),因此现在只有当订单表中不存在该项目已存在的行时才会插入一行。看起来很狡猾,您需要提供更多信息。

可能您的意思是

INSERT INTO user (username, email, address, items)
SELECT username, email, address, items FROM (
   SELECT '$username1' username, '$email1' email, '$address1' address, '$items1' items
     FROM DUAL
   UNION ALL
   SELECT '$username2' username, '$email2' email, '$address2' address, '$items2' items
     FROM DUAL
   UNION ALL
   SELECT '$username3' username, '$email3' email, '$address3' address, '$items3' items
     FROM DUAL
) a
WHERE NOT EXISTS(SELECT 1 FROM order WHERE a.items = items)

如果您的表“订单”在“项目”列上定义了唯一键,那么是的,当您尝试插入新项目时,插入将失败。更好的做法是首先查询订单表以检查该项目是否已存在,如果不存在,则创建它。

就像 AdriftUniform 所说,需要更清晰的信息!

This seems like Oracle syntax... Is it?

INSERT INTO user (id, username, email, address, items)

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.

INSERT INTO user (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)
  • You need 'FROM DUAL' in each union-part
  • Your WHERE-clause only affects the 3rd select
  • It also makes not much sense: 'select this row if there are no rows in the table order'. You will need to specify your exclusion. I added a join (WHERE a.items = items), so now a row will only be inserted when there exists no row in the order table where the item already exists. It seems dodgy, you need to provide more info on this.

Probably you meant

INSERT INTO user (username, email, address, items)
SELECT username, email, address, items FROM (
   SELECT '$username1' username, '$email1' email, '$address1' address, '$items1' items
     FROM DUAL
   UNION ALL
   SELECT '$username2' username, '$email2' email, '$address2' address, '$items2' items
     FROM DUAL
   UNION ALL
   SELECT '$username3' username, '$email3' email, '$address3' address, '$items3' items
     FROM DUAL
) a
WHERE NOT EXISTS(SELECT 1 FROM order WHERE a.items = items)

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文