There are two tables:
Table1 : UserID Name Job
Table2 : BookID Book Car UserID
I load these two tables in one 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:
SqlCommand com = new SqlCommand("delete from Table2 where BookID=#BookID)",con);
but not work,
How can I do it?
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.
If you want to delete users books, do this:
delete from Table2 where userid in (user_id1,user_id1, etc .....);
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:
Delete From Table2 where bookid in (book_id1,book_id2, etc ..);
Or Delete From Table2 where bookid =? //according your development language, you set "?" parameter. I don't know C# syntaxe.
You have to use the below way to delete the rows
DELETE FROM table2
WHERE userid = (SELECT userid
FROM table1);
Are you expecting something like this?
DELETE FROM B WHERE BOOKID IN (SELECT BOOKID FROM B,A WHERE B.USERID=A.USERID AND B.BOOK='ABCD');
Related
I have 3 tables:
maintable(id, serialno, col3, col4, col5, ..., col10)
table1(t1_id, serialno, t1_type, t1_color)
table2(t2_id, serialno, t2_base, t2_price)
maintable's Primary Key is id and serialno is UNIQUE.
Table1's Primary Key is t1_id and table2's is t2_id.
Table1 and Table2 serialno are Foreign Keys that reference MainTable's serialno.
maintable has a one to many relationship with both table1 and table2.
What I want to do is join these 3 tables in a DataTable.
I first thought that it would be simple and I tried: "SELECT * FROM maintable INNER JOIN table1 ON maintable.serialno = table1.serialno INNER JOIN table2 ON maintable.serialno= table2.serialno WHERE maintable.id = 200";
The problem with the result is that if table1 has 3 rows and table2 has 4 rows then my DataTable becomes 12 rows(3x4). What I want to do in this instance is just get 4 rows.
table1 and table2 columns don't have anything to do with each other and they only have to match maintable's serialno.
In case that I'm not being understood, I want to select the rows of table1 and table2 that match maintable's serialno and add them to the right of maintable without them getting duplicated.
Edit: Sorry, I had written accountno instead of serialno in some cases.
SELECT * FROM
maintable m
INNER JOIN (
SELECT t1.serialno, t1.t1_type, t1.t1_color, null as t2_base, null as t2_price
FROM table1 t1
UNION
SELECT t2.serialno, null as t1_type, null as t1_color, t2.t2_base, t2.t2_price
FROM table2 t2
) t ON m.serialno = t.serialno
ORDER BY m.serialno
This will do what you're asking for: return number of rows in t1 + number of rows in t2, rather than rows in t1 x number of rows in t2. Fiddle. This may not perform so hot if you have a large amount of data.
Now that you know how it's done, don't do it.
The real question is why is this a requirement? What are you really trying to accomplish here? This is not a meaningful way to combine the data from the two child tables, given their relationships. T1 and t2 are different tables and not keyed to each other for a reason: they aren't meant to combine their data like this.
The only new data I can imagine extracting from this kind of query is the total count of rows in both t1 and t2 for a given serial number. But there are much better ways to get this information than selecting the rows like this. If you need both t1 and t2 data and duplicates are throwing you off, odds are good that you should be making two separate SELECT statements instead of trying to combine everything.
SELECT
maintable.accountno
FROM
maintable
INNER JOIN table1 ON
maintable.accountno = table1.accountno
INNER JOIN table2 ON
maintable.accountno = table2.accountno
WHERE
maintable.id = 200
GROUP BY
maintable.accountno
I have three tables in SQL Server where I need to combine all matching rows from all tables into a fourth MergedTable that will contain all the columns from the three individual tables based on the U_ID column.
Is there a way of doing this via T-SQL in a stored procedure, or should I just create a loop function in C#?
Bottom line is this is going to be executed from a command from a website, so it needs to be something I can encapsulate into an MVC project or component.
Here is an example of the tables.
Table 1:
U_ID ClientNumber OrderDate Amount
---------------------------------------------
BB000Kw 1920384 5/14/2013 1093.39
AA000bM 3839484 12/8/2012 584.42
AA000gH 8294848 2/28/2014 4849.38
AA000md 3849484 4/31/2013 590.84
AA000mF 3998398 3/29/2013 448.82
AA000mG 9944848 11/28/2014 98.85
AA000mn 0292938 10/31/2012 300.48
Table 2:
U_ID Name Date
------------------------------------------
AA000bM "Krivis, Jeffrey" 7/1/2002
AA000bv "Saydah, Michael" 7/30/2002
AA000cA "Byrne, Richard" 4/21/2003
AA000dd "McNeil, Joseph" 6/10/2003
AA000dH "Greenberg, Arnold" 1/16/2003
AA000gH "Rich, Elwood" 7/5/2003
AA000id "O'Neill, Robert J." 11/20/2002
AA000jf "Patsey, Richard" 4/22/2003
AA000jr "Jones, Arthur" 7/1/2002
AA000jU "Toff, Ronald" 7/15/2002
AA000k4 "Anderson, Carl" 8/14/2002
BB000Kw "Wilson, Sam" 3/9/2003
Table 3:
U_ID Name
-----------------------------
AA000bM Acme Company
AA000jr Stockwell Industries
BB000ke Gensen Motors
BB999di Falstaff Cards
BB000dl B and R Printing
BB000Kw Go Golf Carts
AA000gH Rich's Sandwiches
Resulting merged table
U_ID ClientNumber OrderDate Amount CustomerName JoinDate CompanyName
-------------------------------------------------------------------------------------------------------
BB000Kw 1920384 5/14/2013 1093.39 "Wilson, Sam" 3/9/2003 Go Golf Carts
AA000bM 3839484 12/8/2012 584.42 "Krivis, Jeffrey" 7/1/2002 Acme Company
AA000gH 8294848 2/28/2014 4849.38 "Rich, Elwood" 7/5/2003 Rich's Sandwiches
Table 1 is the master table that the others are matched to. You can see from the result that there will be only a subset of all the tables based on those that are matched from Table 1.
I'll be using MVC with the Entity Framework 6 and Linq-to-Entities, but if a T-SQL script is more efficient, then I should probably use that instead.
Which is the better way to go to get this result?
If you want to create a new table you can use SELECT ... INTO ... FROM ... query. In your case it would look like this:
SELECT t1.U_ID, t1.ClientNumber, t1.OrderDate, t1.Amount,
t2.Name as CustomerName, t2.Date as JoinDate,
t3.Name as CompanyName
INTO dbo.ResultingMergedTable
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.U_ID = t2.U_ID
INNER JOIN Table3 t3 ON t1.U_ID = t3.U_ID
Keep in mind that if you are looking at really big data table this will take a lot of time to execute.
You can create a 4th table to do what you mentioned but if you are using sql you can create a view to do the same thing. A view is a virtual table. We use this when we partition data as well as make a detailed record like described above.
http://msdn.microsoft.com/en-us/library/ms187956.aspx
http://www.sqlinfo.net/sqlserver/sql_server_VIEWS_the_basics.php
CREATE VIEW DetailView AS
(
SELECT
-- table1
t1.U_ID,
t1.ClientNumber,
t1.OrderDate,
t1.Amount,
-- table2
t2.Name,
t2.Date as [JoinDate],
-- table3
t3.Name as [Company]
FROM
table1 t1
LEFT JOIN
table2 t2
ON t1.U_ID = t2.U_ID
LEFT JOIN
table3 t3
ON t1.U_ID = t3.U_ID
WHERE
t1.U_ID = t2.U_ID
and
t1.U_ID = t3.U_ID
)
I have 2 table :
table1: name, family, cityID
table2: cityID, cityName
I have a gridview that it's datasource is:
select * from table1, table2 where table1.cityID = table2.cityID
I disable autoCreatColumns
I add 3 columns to my datagrid.
in first column I fix the data property name to name
in second column I fix the data property name to family
but in third colum when I fix the data propery name to table2.cityName It dosent work...
I want to show the city name in third column.
Its an example but in fact, I have cityName in anouther table that I used, and if I use just cityName, makes error
can any body help me?
Thanks...
try to change
select * from table1, table2 where table1.cityID = table2.cityID
To
select table1.name as Name,table1.family as Family,table2.cityName as City from table1, table2 where table1.cityID = table2.cityID
SELECT name,family,cityName from table1 INNER JOIN table2 ON table1.cityID = table2.cityID
As you are doing inner joins with city ids on both tables..U will get matched Table IDs with name,citynames
You don't need to write table2.cityName, just write cityName and it should work.
I have 2 tables :
Teachers :
teacher_id
teacher_name
=============
Timetable:
tt_id
tt_teacher_id
tt_term_id
tt_day_id
tt_hour_id
=====================================
I want 2 type SQL select query :
1)(this query for select teachers and set into combobox) select teachers_name that not exists in records with special parameters in timetable
tt_term_id=parameter1
tt_day_id =parameter2
tt_hour_id=parameter3
teacher names comes from teachers table based on above conditions in timetable table .
2) select teachers_name that not exists in records with special parameters in timetable
tt_term_id=parameter1
tt_day_id =parameter2
tt_hour_id=parameter3
tt_teacher_id !=parameter4
teacher names comes from teachers table based on above conditions in timetable table .
Thanks a lot
You can join the tables based on the teacher id and use whatever conditions you want.
SELECT DISTINCT teacher_id, teacher_name
FROM Teachers
INNER JOIN Timetable
ON teacher_id = tt_teacher_id
WHERE tt_term_id = parameter1
AND tt_day_id = parameter2
AND tt_hour_id = parameter3
ORDER BY teacher_name
The other one you will be able to come up with yourself :-)
If you want teachers who do not have any timetable records then you can do:
SELECT DISTINCT teacher_id, teacher_name
FROM Teachers
LEFT JOIN Timetable
ON teacher_id = tt_teacher_id
WHERE tt_id is NULL
ORDER BY teacher_name
I have to delete a product and its images. Images are in seperate table and productId is acting as foreign key.
Here is the single queries to do so.
string deleteImages = #"DELETE FROM [ProductsImages] WHERE ProductId IN (SELECT ProductId FROM [Products] WHERE ProductId = #ProductId)";
string deleteProduct = #"DELETE FROM [Products] WHERE ProductId = #ProductId";
db.ExecuteNonQuery(deleteImages);
db.ExecuteNonQuery(deleteProduct);
I am not getting the way to avoid writing 2 different queries, Is there any help to delete cascade without alter command?
This is something you can define in the database. Just define the foreign key relationship between Products and ProductsImages as "ON DELETE CASCADE". Deleting the product will then automatically delete all associated ProductImages.
If you don't want to change the index, you can avoid two round trips by performing multiple operations in one command:
string deleteStuff = #"
DELETE FROM [ProductsImages] WHERE ProductId = #ProductId;
DELETE FROM [Products] WHERE ProductId = #ProductId;"
db.ExecuteNonQuery(deleteStuff);
DELETE p.*, i.*
FROM Products p
LEFT JOIN ProductImages i ON p.ProductId = i.ProductId
WHERE p.ProductId = #ProductId
EDIT
Not supported by SQL Server