简体   繁体   English

删除多个表中的多行

[英]Delete Multiple rows in several tables

I have 3 tables in an SQL database:我在 SQL 数据库中有 3 个表:

  1. Animal Status动物状况
  2. Animals动物
  3. Cart大车

I want to be able to delete several items from the Animals and Animal Status tables based on the cart.我希望能够根据购物车从AnimalsAnimal Status表中删除几个项目。

What's the same between all the tables is the AnimalID which is the primary key in all the tables and based on that I'm supposed to delete them.所有表之间的相同之处是 AnimalID,它是所有表中的主键,基于此我应该删除它们。 But at the same time I need to delete it for someone with a specific email.但同时我需要为拥有特定电子邮件的人删除它。

Example is if I have someone with the email 123@gmail.com and this person has 2 animals in his cart 1 with the ID 123 and the other with the id 456 I need to delete these 2 specific animals from the other 2 tables.例如,如果我有一个电子邮件为 123@gmail.com 的人,并且此人的购物车 1 中有 2 只动物,ID 为 123,另一只动物的 ID 为 456,我需要从其他 2 个表中删除这 2 只特定动物。

I know how to do it for 1 table but not when I have several tables that depend on each other along with them depending on the specific email when there are also other emails and animals in the cart.我知道如何为 1 张桌子做这件事,但当我有几张桌子相互依赖时,根据特定的电子邮件,当购物车中还有其他电子邮件和动物时,我就不知道了。

Here is how it works on the outside:这是它在外部的工作方式:

x= AnimalStatus.Delete(AdoptCart.GetAnimalId(Session["email"].ToString()).ToString());
            if (x > 0)
            {
                x = Animal.Delete(AdoptCart.GetAnimalId(Session["email"].ToString()).ToString());
                if (x > 0)
                {
                    AdoptCart.RemoveAllbyEmail(Session["email"].ToString());

Here are all the deletion codes in the order they work:以下是按工作顺序排列的所有删除代码:

Removes the AnimalStatus based on the animals ID:根据动物 ID 删除 AnimalStatus:

static public int Delete(string id)
    {
        int rowsAffected;
        string strSql = string.Format("delete from AnimalStatus where AnimalID='" + id + "'");
        rowsAffected = (int)dataservice.ExecuteNonQuery(strSql);
        return rowsAffected;
    }

Removes an animal from the animal table based on the animal's ID:根据动物的 ID 从动物表中删除动物:

 static public int Delete(string id)
        {
            int rowsAffected;
            string strSql = string.Format("delete from Animal where AnimalID='" + id + "'");
            rowsAffected = (int)dataservice.ExecuteNonQuery(strSql);
            return rowsAffected;
        }

Removes all animals from the Cart Table:从购物车表中删除所有动物:

static public int Remove(string AnimalID) 
    {
        int rowsAffected;
        string strSql = string.Format("delete from Cart where AnimalID='" + AnimalID + "'");
        rowsAffected = (int)dataservice.ExecuteNonQuery(strSql);
        return rowsAffected;
    }

Issue with this code is that all of that only deletes 1 animal and I need several removed.这段代码的问题是所有这些只删除了 1 只动物,我需要删除几只。

I have tried creating a statement like this, but I didn't manage to make it work.:我曾尝试创建这样的语句,但我没有设法使它起作用。:

Delete Animal.*, AnimalStatus.*

FROM     Animal INNER JOIN
                  AnimalStatus ON Animal.AnimalID = AnimalStatus.AnimalID INNER JOIN
                  Cart ON Animal.AnimalID = Cart.AnimalID
WHERE  (Cart.UserEmail = N'Email')

Based on the example I gave in words the ones that are supposed to be deleted are the following marked in //removed .根据我用文字给出的示例,应该删除的内容如下在//removed标记。

Cart Table
 | AnimalID | Email_Addr |
--------------------------------------
| 123 | 123@gmail.com | //removed
| 456 | 123@gmail.com | //removed
| 765 | jj@gmail.com  |
| 343 | bb@gmail.com  |
| 256 | cc@gmail.com  |

Animal Status Table
 | AnimalID | Vaccinated |
--------------------------------------
| 123 | Yes | //removed
| 456 | Yes | //removed
| 765 | No  |
| 343 | No  |
| 256 | No  |

Animals Tables
 | AnimalID | Age |
--------------------------------------
| 123 | 3 | //removed
| 456 | 4 | //removed
| 765 | 3 |
| 343 | 7 |
| 256 | 10|

Looking at your current work there are a few things you could do.看看你目前的工作,你可以做一些事情。 You could rewrite your query logic like you're asking, but what I'm not seeing in your example is primary/foreign key relationships.您可以按照您的要求重写查询逻辑,但我在您的示例中没有看到的是主键/外键关系。 So cascade won't work with how you currently have your tables set up.因此,级联不适用于您当前设置表格的方式。 This is a good write up of cascade delete.这是级联删除的好文章。 Cascade Delete 级联删除

So if you don't want to rewrite everything you really could just do all of this in your C# code.因此,如果您不想重写所有内容,您真的可以在 C# 代码中完成所有这些操作。 All you'd have to do is loop through the cart table and get all of the id's that have the e-mail "123@gmail.com" and call your three delete statements.您所要做的就是遍历购物车表并获取所有包含电子邮件“123@gmail.com”的 ID,然后调用您的三个删除语句。 You'd just need to write a method to grab the id's and call the delete methods until the id variable is empty.您只需要编写一个方法来获取 id 并调用 delete 方法,直到 id 变量为空。

It just depends where you want all of the logic to exist, in sql or in your C# code.这仅取决于您希望所有逻辑存在的位置,在 sql 中还是在您的 C# 代码中。 If you're using Entity Framework, whether code first or database first, you have options to use LINQ Query syntax or extension methods.如果您使用实体框架,无论是代码优先还是数据库优先,您都可以选择使用 LINQ 查询语法或扩展方法。 Just depends on preference really.只是真的取决于偏好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM