C# Executing multiple SQL Statements and display result n GridView - c#

I am using VS2005 and SQL Server 2005.
I am trying to execute multiple sql statements on two sql tables, meaning which I need to do more than a single check on the two tables.
The two tables are:
Table1: UserID, Username.
Table2: UserID, Status.
The following are the checks that I need to perform, but I do not know what is the exact SQL query I need.
Users that exist in Table1 should exist in Table2
Users that exist is Table1 should not have STATUS=DELETE in Table2
Users that do not have STATUS=DELETE in Table2 should exist in Table1
May I know how do I form this checks in to SQL queries and execute them in my VS Sqldatasource?
After which store the results in a variable and display them in GridView table.
Thank You very much for the help.

So you have 3 cases you want to catch:
User exists in table1 but not in table2
User is marked as DELETE in table2 but still exists in table1
User is NOT marked as DELETE in table2 but doesn't exist in table1
These three scenarios are covered in the WHERE clause below in that order.
SELECT *
FROM table1 t1
FULL OUTER JOIN table2 t2 ON t1.userid = t2.userid
WHERE (t2.userid IS NULL AND t1.userid IS NOT NULL)
OR (t2.status = 'DELETE' AND t1.userid IS NOT NULL)
OR (t2.userid IS NOT NULL AND t2.status <> 'DELETE' AND t1.userid IS NULL)
Edit: In response to the OP's comment, here is a modified version that will insert data into table3 based on case #1. Assume that table3 is a table that has two columns, userID and problem.
INSERT INTO table3
SELECT userID, 'No Matching Table2 Record'
FROM table1 t1
FULL OUTER JOIN table2 t2 ON t1.userid = t2.userid
WHERE (t2.userid IS NULL AND t1.userid IS NOT NULL)

You want to join table 1 and 2 on UserID and exclude delete status. Then use the results in your grid view.
select t1.UserID, t1.Username
from Table1 t1
join Table2 t2 on t1.UserID = t2.UserID
where t2.Status != 'DELETE'

Related

How do you SELECT from Two Tables in MySql

I have two MySql tables which I need to select a column from one, and where the results are used to select from another table. I know how to do it as two different select statements. However, I believe I can do it as a single statement but have no idea how.
Table one has two columns the second column has values which are also found in table two. I need to select all rows in table two which has the same values as those found in table one and where another column value is 0.
Any ideas how to go about doing this?
Use Join On tables to get columns form both table using query as
SELECT column_list
FROM table_1
LEFT JOIN table_2 ON
table_1.column = table_2.column;
Try to use Join query
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

Display data from Table 1 after verifying column from table 1,table 2 and table3

I have three table, table a, b, c
table a does not have any link with table c, table b have one same column in table a and one column same as in table c
How to display data in table a by filtering the two column from table a and table B, table b and c
If you just want to filter a, I would recommend exists:
select a.*
from a
where exists (select 1
from b join
c
on b.colb = c.colb
where b.cola = a.cola
);
The advantage of this approach over join is that it does not return duplicate values when there are multiple matches. Your question specifies that you want to "display a" filtered by the other tables. This does exactly what you are specifying.
Two JOINs would achieve what you want efficiently. For instance:
SELECT TableA.Value1,
TableA.Value2
FROM TableA
JOIN TableB
ON TableB.KeyColumn = TableA.TableBKey
JOIN TableC
ON TableC.KeyColumn = TableB.TableCKey
This returns records in TableA with a reference to a record in TableB, which in turn has a reference to a record in TableC
There are other alternatives using LEFT JOIN (return all records in TableA, and referenced records from TableB if they exist, as opposed to only records in TableA that reference records in TableB), and OUTER JOIN etc
Wikipedia actually has some decent JOIN documentation, along with good explanatory Venn diagrams.

How to copy data from one column in certain table to same named column in another table in SQL Server

I have two SQL Server tables sub_aminer_author2paper and sub_aminer_paper, and there is a common column pid.
Another column aid exists in only one table i.e. sub_aminer_author2paper.
Now I have to copy column aid from table sub_aminer_author2paper to newly created column aid in the table sub_aminer_paper, whereas the column pid in table sub_aminer_paper should match the pid in table sub_aminer_author2paper
I have tried this query as:
insert into sub_aminer_paper (sub_aminer_paper.aid)
(select sub_aminer_author2paper.aid
from sub_aminer_author2paper
INNER JOIN sub_aminer_paper ON sub_aminer_paper.pid = sub_aminer_author2paper.pid)
But it not works as required, it has inserted all the aid values at end of the table as
Please help!
Add a column named aid with null in sub_aminer_paper table. Then simply update it.
UPDATE A
SET A.aid = B.aid
FROM sub_aminer_paper A
INNER JOIN sub_aminer_author2paper B ON A.pid = B.pid
I think you are describing an UPDATE scenario here, not an INSERT, if I understand you correctly.
Update statements can include a from part with multiple tables joined like this:
UPDATE sub_aminer_paper
SET aid = sub_aminer_author2paper.aid
FROM sub_aminer_author2paper
INNER JOIN sub_aminer_paper ON sub_aminer_paper.pid = sub_aminer_author2paper.pid
This statement will update the aid column for existing rows in sub_aminer_paper with the aid value found in sub_aminer_author2paper for matching pid.

query to dispaly from two tables with negation

I have two listboxes,listbox1 and listbox2, and two tables in a DB, table1 and table2. Both tables contain a column called 'Colour'. I want to display all 'Colours' from table1 in listbox1 which I‘m able to do. But in listbox2 I want to display 'Colours' from Table2 but it must not be present in the Table1 'Colours'. So how do I write a query for it?
This is what I have been trying and its not working:
SELECT Table2.Colour
FROM Table1 CROSS JOIN Table2
WHERE (Tabel1.Colour! = Table2.colour)
Error Message is — multi-part identifier Tabel2.Colour could not be found
This should work with a LEFT JOIN:
SELECT Table2.Colour
FROM Table2
LEFT JOIN Table1 ON Tabel1.Colour = Table2.colour
WHERE Table1.Colour IS NULL
You could also use NOT IN or NOT EXISTS, I just prefer the LEFT JOIN / IS NULL syntax.
For example:
SELECT Colour
FROM Table2
WHERE Colour NOT IN (SELECT Colour FROM Table1)
If your version of RDBMS implements EXCEPT clause,
you can do the following:
SELECT Colour FROM Table2
EXCEPT
SELECT Colour FROM Table1
I'm not saying that this is better than JOIN. Just another option.

How would I pull data from two tables using one variable that both tables have?

Lets say I have a variable 'userid', I want to select from aspnet_Membership AND aspnet_AccountProfile tables. They both have the column userid, I just want to be able to make a statement like SELECT * FROM aspnet_AccountProfile, aspnet_Membership WHERE UserId=#UserId and it gets the records with the matching user id for BOTH tables. how do I do this? Thank you!
That is called a JOIN:
There are several basic types of join based on what data exactly you want. These are related to set theory/relational algebra. I'll list the most common ones:
INNER JOIN
Use this when you want to return every possible combination of rows where both tables have a matching UserId. Some rows in either table may not get returned in an inner join.
SELECT * FROM aspnet_AccountProfile INNER JOIN aspnet_Membership
ON aspnet_AccountProfile.UserId = aspnet_Membership.UserId
Another way of writing an INNER JOIN (Which I wouldn't encourage if you want to understand joins) is:
SELECT * FROM aspnet_AccountProfile, aspnet_Membership
WHERE aspnet_AccountProfile.UserId = aspnet_membership.UserId
Of course, to select the specific UserId you want, you add a condition on either table eg:
AND aspnet_AccountProfile.UserId = #UserId
OR
AND aspnet_Membership.UserId = #UserId
Either of those two will work fine for an inner join.
LEFT OUTER JOIN
Use this when you want to return all rows from the first table in your query, and every combination where the UserId in the second table matches the first. Some rows in the second table (Membership, in this case) may not get returned at all.
SELECT * FROM aspnet_AccountProfile LEFT JOIN aspnet_Membership
ON aspnet_AccountProfile.UserId = aspnet_Membership.UserId
You have to use the left column to narrow down your criteria in this case, or it will automatically get converted to an INNER JOIN.
WHERE aspnet_AccountProfile.UserId = #UserId
RIGHT OUTER JOIN
This is fairly uncommon, because it can usually be written as a LEFT outer join. It's like a left outer join, but all rows from the second table in the relation are returned instead of the first.
SELECT * FROM aspnet_AccountProfile RIGHT JOIN aspnet_Membership
ON aspnet_AccountProfile.UserId = aspnet_Membership.UserId
FULL OUTER JOIN
Use this if you need to relate all the rows with a matching UserId in AccountProfile to the corresponding rows in Membership, but also need to know which rows in either table don't have a match in the other one.
SELECT * FROM aspnet_AccountProfile FULL OUTER JOIN aspnet_Membership
ON aspnet_AccountProfile.UserId = aspnet_Membership.UserId
Getting results for only a single user is a little trickier in a FULL OUTER JOIN. You have to specify that a NULL or the correct value is okay in either table.
Hi,
you can do it by using
"SELECT * FROM aspnet_AccountProfile
ap, aspnet_Membership m WHERE
ap.UserId=m.UserId ANB
ap.UserId=#UserId"
you can do this by inner join.
Here is the example,
Select aspnet_Membership.*, aspnet_AccountProfile.* from aspnet_AccountProfile
inner join aspnet_Membership on aspnet_Membership.userid = aspnet_AccountProfile.userid
where aspnet_Membership.UserId=#UserId
This will get only the record whem userid is common in both the table.
If you want to get the record which are in 1 table and may or may not be in other then you must user the left join
that is
Select aspnet_Membership.*, aspnet_AccountProfile.* from aspnet_AccountProfile
left join aspnet_Membership on aspnet_Membership.userid = aspnet_AccountProfile.userid
where aspnet_Membership.UserId=#UserId
You can use a Join.
Something like:
Select * from aspnet_AccountProfile INNER JOIN aspnet_Membership
ON aspnet_AccountProfile.UserId = aspnet_Membership.UserId
Where aspnet_AccountProfile.UserId = #UserId
Thanks,
Vamyip
You can use a simple inner join for this.

Categories

Resources