简体   繁体   English

从连接在一起的两个sql表中删除行

[英]Delete row from two sql tables that join together

There are two tables: 有两个表:

Table1 : UserID Name Job
Table2 : BookID Book Car UserID

I load these two tables in one wpf datagrid: 我将两个表加载到一个wpf datagrid中:

da.SelectCommand = new SqlCommand("select Table1.UserID, Table1.Name, Table1.Job, Table2.Book, Table2.Car from Table1 inner join Table2 on Table1.UserID = Table2.UserID");

I want to delete one row from Table2 by DataGrid: 我想通过DataGrid从Table2删除一行:

 SqlCommand com = new SqlCommand("delete from Table2 where BookID=@BookID)",con);

but not work, 但没有用

How can I do it? 我该怎么做?

You have to use the below way to delete the rows 您必须使用以下方式删除行

DELETE FROM table2
WHERE  userid = (SELECT userid
                 FROM   table1);  

您是否期望这样的事情?

DELETE FROM B WHERE BOOKID IN (SELECT BOOKID FROM B,A WHERE B.USERID=A.USERID AND B.BOOK='ABCD'); 

You say "It must delete a book from a certain user not all books.". 您说“它必须从某个用户而不是所有书中删除一本书”。 You have to know user id for which the books must be delete. 您必须知道必须删除其书籍的用户ID。

If you want to delete users books, do this: 如果要删除用户书籍,请执行以下操作:

delete from Table2 where userid in (user_id1,user_id1, etc .....); 从表2中删除用户ID所在的位置(用户ID1,用户ID1等...); But you are large rows to delete, use bulk delete mechanism. 但是您要删除大行,请使用批量删除机制。

If you don't have user ids and have book ids, you have to do this: 如果您没有用户ID和书籍ID,则必须执行以下操作:

Delete From Table2 where bookid in (book_id1,book_id2, etc ..); 从表2中删除bookid在其中的bookid(book_id1,book_id2等); Or Delete From Table2 where bookid =? 或者从Table2中删除,其中bookid =? //according your development language, you set "?" //根据您的开发语言,设置“?” parameter. 参数。 I don't know C# syntaxe. 我不知道C#语法。

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

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