Retrieve data from two table - c#

I have two tables tblRegistration(id,name,program,regdno,address) and tblDue(id,regdno,amountdue). What I want is to pass regdno from a textbox and then retrieve the name, program values form tblRegistration and amountdue from tblDue.
What i tried is ,
select t1.name,t1.program, t2.amountdue
from tblRegistration as t1
inner join tblDue as t2 on t2.regdno= t1.regdno;
It returns all values having same regdno in both tables.
Help me on getting those values only whose regdno I provide from textbox.
Sorry for the language. Thank you in advance.

Modifiy your query sql like this:
select t1.name,t1.program, t2.amountdue from tblRegistration as t1 inner join tblDue as t2 on t2.regdno= t1.regdno
where t1.regdno=yourvaluetestbox;

If you haven't any relationship between Registration and Due table. you mustn't join these table. You can Simple run two query to fetch data. the first, from Registration table and the second, from Due table:
select * from tblReg where regdno=...;
select * from tblDue where regdno=...;

SELECT t1.name,t1.program, t2.amountdue FROM tblRegistration AS t1 INNER JOIN tblDue AS t2 ON t2.regdno = t1.regdno
WHERE t1.regdno = 'TextBoxValue';
Please let me know if you need any more clarification.
Thanks

use Where clause at the end of your your query
Where t1.regdno ='TextBox Text' and t2.regdno = 'TextBox Text'
in place of using regdno in two tables you should remove regdno from tblDue Table and Add a Foreign key reference in tblDue for tblRegisteration add regid in tblDue
new query will be :
select t1.name,t1.program, t2.amountdue from tblRegistration as t1
inner join tblDue as t2 on t2.regid= t1.id
where t1.regdno ='TextBox Text'

Related

Linq to entities, How to select all columns and also select custom columns?

I need to be able to do this query in linq
select t1.*, t2.column1, t2.column2
from Table1 as t1 inner join Table2 as t2
Where t2.x = true
the main table we want to get all his columns and some other columns from other tables. Can this be done in linq?
from element in t1
join otherElement in t2
on element.match equals otherElement.match
where t2.x == true
select new {
column1 = otherElement.column1,
column2 = otherElement.column2
//add all the elements of t1 here
}
A join clause takes two source sequences as input. The elements in each sequence must either be or contain a property that can be compared to a corresponding property in the other sequence.
https://msdn.microsoft.com/en-us/library/bb311040.aspx

Query in Ms Access, how to make a chain of joins (with its own criteria) that only show up if the first part of the join exists

I'm trying to make a query that makes a new table from 7 different tables, and then I need to use this query in a C# program, so I wouldn't be able to make 2 queries to do this.
How would I make a join that only shows up if the two parts of the join are equal, and if it does show up, it gets more info from a third table and only really shows up if one of its columns in the third table equals 2.
For example, I have a lot of different joins, and at one part, Table 1, I join it to Table 2. I want everything from table 1 to show up, and only the matching ones from table 2. and if table 1 and 2 match, the info in table 2 only shows up if it fits a certain criteria from table 3, which is join to table 2.
I hope that makes sense?
Thanks!
EDIT
okay the main problem is am I able to set criteria that only happens for a certain join and not the whole query?
From the info you have provided it sounds like you want to left join table 2 to table 1, and inner join table 3 to table 2, using your criteria.
for example, i have alot of different joints, and at one part, Table 1, i join it to Table 2.
Ok, you want Table 1 to join to Table 2.
I want everything from table 1 to show up, and only the matching ones from table 2.
This is called a Left Join from Table1 to Table2. A Left Join returns all records from the left hand table, even if there is no matching record in the right hand table.
So, so far we have
SELECT *
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.ID = T2.ID
and if table 1 and 2 match, the info in table 2 only shows up if it fits a certain criteria from table 3, which is joint to table 2.
This means you want an inner join between Table2 and Table3. An Inner Join returns all records that match both the left and right hand tables. Let's combine this with our first query:
SELECT *
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.ID = T2.ID
INNER JOIN Table3 T3 ON T3.ID = T2.ID
However, this won't work. This only returns records that match all 3 tables because the Inner Join is applied to the result from the first Left Join.
What you need in this circumstance is a derived table. You can get one by using the following query:
SELECT *
FROM #Table1 T1
LEFT JOIN (
SELECT T2.ID as T2ID, T2.HairColor as HairColor, T3.ID as T3ID, T3.Age as Age FROM #Table2 T2
INNER JOIN #Table3 T3 ON T3.ID = T2.ID
WHERE T3.Age = 3
) as T2SubSet ON t1.ID = T2SubSet.T2ID
This is evaluated by creating the subset of table 2/3 that you are looking for and then taking the result of that query and joining it to table 1 with a left join. For a full complete example, you can use the following query:
DECLARE #Table1 TABLE(
ID int,
Name varchar(10))
DECLARE #Table2 TABLE(
ID int,
HairColor varchar(10))
DECLARE #Table3 TABLE(
ID int,
Age int)
INSERT INTO #Table1 values (1, 'John'), (2, 'Mary'), (3,'Sue')
INSERT INTO #Table2 values (1, 'Red'), (2, 'Brown'), (3, 'Black')
INSERT INTO #Table3 values (1, 3), (2, 5), (4,8)
SELECT *
FROM #Table1 T1
LEFT JOIN #Table2 T2 ON T1.ID = T2.ID
INNER JOIN #Table3 T3 ON T3.ID = T2.ID
SELECT *
FROM Table1 T1
LEFT JOIN (
SELECT T2.ID as T2ID, T2.HairColor as HairColor, T3.ID as T3ID, T3.Age as Age FROM Table2 T2
INNER JOIN Table3 T3 ON T3.ID = T2.ID
) as T2SubSet ON t1.ID = T2SubSet.T2ID
I also just noticed you said:
columns in the third table equals 2.
This can be done by adding another 'On' statement like 'T3.Column = 2' Using my last query above, it would be
SELECT *
FROM #Table1 T1
LEFT JOIN (
SELECT T2.ID as T2ID, T2.HairColor as HairColor, T3.ID as T3ID, T3.Age as Age FROM #Table2 T2
INNER JOIN #Table3 T3 ON T3.ID = T2.ID AND T3.Age = 2
) as T2SubSet ON t1.ID = T2SubSet.T2ID

Multiple table join with multiple table where clause

I have a query that's something like this.
Select a.*
from table1 a
inner join table2 b on a.field1 = b.field1
inner join table3 c on b.field2 = c.field2
where b.field4 = beta and c.field5 = gamma.
On LINQ, I tried to do that this way:
var query = (from a in table1
join b in table2 on a["field1"] equals b["field1"]
join c in table3 on b["field2"] equals c["field2"]
where (b["field4"] == beta && c["field5"] == gamma)
select a).ToList();
But for some reason, when I try to do this I get an error that says that the entity "table2" doesn't have the field Name = "field5", as though as the where clause was all about the last joined table and the other ones were unaccessible. Furthermore, the compiler doesn't seem to notice neither, because it lets me write c["field5"] == gamma with no warning.
Any ideas? Am I writing this wrong?
Thanks
See these links:
How to: Perform Inner Joins (C# Programming Guide)
What is the syntax for an inner join in linq to sql?
Why you don't create View in database, and Select your data from View in LINQ?

How to compare/match values in sql server in two different tables

I need to have two tables.
First table (tblStudent) has fields student_id,firstname,lastname,class,rollno.
Second table(feeType) has fields class,admissionFee, tuitionFee etc.
I want to show the different fee for each class.
Like class 5 pays 600 as tuition fee where as class 6 will pay 700.
Eg. If class is 5 in first table then only the class 5 row in feeType should be shown ? How do I design tables as such? How do I join two tables? I will be using sql server 2008 and c# as frontend. Thanks from a novice.
This is the simplest type of INNER JOIN:
SELECT *
FROM tblStudent AS s
INNER JOIN feeType as f ON s.class = f.class
Your INNER JOIN would only return matching records,for non-matching records you could use a FULL JOIN and WHERE criteria to check.
SELECT *
FROM tblStudent AS s
INNER JOIN feeType as f ON s.class = f.class where s.class=null;
Visit here
This will list all the class names and their fees.
SELECT
s.class,
f.admissionFee as admission_fee,
f.tuitionFee as tuition_fee
FROM
tblStudent as s
JOIN feeType as f ON s.class = f.class
GROUP BY
s.class;
I hope this will help you to get the exact result set.

SQL query help in where clause

string sql1 = "select jname, jcode
from heardt,judge,main
where heardt.jud1 = judge.jcode and main.fil_no=heardt.fil_no and ..
main.fil_no= ";
I have a form in which user enters a reg_no on entering reg_no only name and address is displayed, in the same table there is fil_no which i want to be used in my above query to link to another table.
how can i specify it in my above query,in the third AND condition? please guide me.
You are talking about JOINing tables I believe? Joins are very simple and are written as thus :-
SELECT t1.column1,t1,column2,t2.column1,t2.column2
FROM table1 t1
JOIN table2 t2
ON t1.key1 = t2.key1
WHERE .....
You can join as many tables as you require and have multiple JOIN types.
See more here :-
http://msdn.microsoft.com/en-us/library/ms191472.aspx

Categories

Resources