SQL query help in where clause - c#

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

Related

C# SqlCommandBuilder , CommandUpdate - how to write correct update based on select with outer join tables

I want is to update 2 fields: p.FlagaWaznosci and p.Notatka
My select looks like:
Select DISTINCT p.id,p.Model_Number,p.Product_Name,p.Website_Link,p.Entry_date,p.LastUpdate_date,p.PrzydzialRozmiarow_ID,p.FlagaWaznosci,p.Notatka,pr.NazwaRozmiarowki,wd.LINK_StockX
from Products p with(nolock)
left outer join Widok_Model_Sklep_Stockx_Linki wd with(nolock) on wd.Product_ID = p.id
left outer join PrzydzialRozmiarow pr with(nolock) on pr.id = p.PrzydzialRozmiarow_ID
inner join Shops s with(nolock) on s.ID = p.Shop_ID
There is just outer joins to get correct data that I need to be displayed in gridview. And now when values p.FlagaWaznosci or p.Notatka is changed I want to save update in my database.
I try to use
//loads dataand fill to gridview
DataTable WszystkieProduktyDlaDanegoSklepu;
SqlDataAdapter sda555123 = new SqlDataAdapter("here is my select", conn123);
sda555123.Fill(WszystkieProduktyDlaDanegoSklepu);
//later update table Prooducts and save changed on p.Notatka and p.FlagaWaznosci
cmdbl = new SqlCommandBuilder(sda555123);
cmdbl.ConflictOption = ConflictOption.OverwriteChanges;
sda555123.Update(WszystkieProduktyDlaDanegoSklepu);
But this way I have error
So I searched a lot and found: I have to write own CommandUpdate.
So ... sda555123.UpdateCommand and I don't have idea how can I write own update for it in update command.
The update in SQL Server should looks like:
Update Products
set FlagaWaznosci = #Flagawaznosci from my sda555123,
Notatka = #Notatka from my sda555123
where id = # p.ID from my sda555123
How my command update should looks like here?
EDIT 1 :
i try added : WszystkieProduktyDlaDanegoSklepu.PrimaryKey = new DataColumn[] { WszystkieProduktyDlaDanegoSklepu.Columns["id"] }
but nothing . Still this error.
I would solve the problem by changing the approach instead of mutating the update command of the SqlDataAdapter.
Given that Products.id in your query is unique within the result set:
1- Create a temporary table (local or global), having its columns same as the result of the query with id as primary key.
2- Insert data into the temporary table using your select statement.
3- DataAdatper.selectQuery.commandText is set to "select * from TempTable"
4- The update command is now based on a simple select statement, consequently any change in the datagridview/datatable can be updated to the temptable using dataadapter.update(datatable)
5- As for the final database update, you could use the below statement
Update Prd
set Prd.FlagaWaznosci = TempTable.FlagaWaznosci ,Prd.Notatka = TempTable.Notatka etc.. all the fields that need to be updated
from my Products as Prd
Inner Join TempTable on TempTable.id = Prd.id
Note that the update in (5) will affect all rows, even unchanged ones.
To address this issue you can proceed as below
1- Save changed ids in a list.
List<string> lst = new List<string>();
foreach(DataRow dr in datatable.GetChanges(DataRowState.Modified))
{
lst.add(dr["id"].ToString());
}
2- Convert your list to a string value to be concatenated with the query in (5)
String strchange = String.Join(",",lst); //will give you id1,id2,...
//The update query becomes
Update Prd
set Prd.FlagaWaznosci = TempTable.FlagaWaznosci ,Prd.Notatka =
TempTable.Notatka etc.. all the fields that need to be updated
from my Products as Prd
Inner Join TempTable on TempTable.id = Prd.id
Where Prd.id In ( strchange )
Kindly update your tables separately because in join you just seen two or more than two tables into one table form . but you cant do any crud operation on

Retrieve data from two table

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'

SQL JOIN on three tables using a common table

I have to make a join on 3 three table, the scheme is following:
1. Request_Send table have: RequestID(FK),DonorID(FK),SendRequestID(PK)
2. Donor table have Donor,ID(PK),Name,etc...
3. Blood_Request table have RequestID(PK),etc...
Now I want to make join in which I could select some columns from Donor, some columns from Request. so how could I do this?
my present query is:
string show = "
SELECT Blood_Request.Date,Blood_Request.Time,Blood_Request.R_Name,R_Address,R_Phone
FROM Request_Send INNER JOIN
Blood_Request ON Blood_Request.RequestID=Request_Send.RequestID INNER JOIN
Donor ON Request_Send.DonorID=Donor.DonorID
Where D_Emial='" + Session["UserID"];
Please Help and thanks in advance.
Your problem is Donor.DonorID=Request_Send.DonorID
try this query your problem is solved.
string show ="
SELECT Blood_Request.Date,Blood_Request.Time,Blood_Request.R_Name,R_Address,R_Phone
FROM Request_Send INNER JOIN
Blood_Request ON Blood_Request.RequestID=Request_Send.RequestID INNER JOIN
Donor ON Donor.DonorID=Request_Send.DonorID
WHERE D_Emial='" + Session["UserID"] + "'";
You need to provide talbe name from where D_Emial will select data like
Where Request_Send.D_Emial ='something';
Donor.DonorID doesn't exist. Try Donor.ID in your join
string show = "select bld_req.Date,bld_req.Time,bld_req.R_Name,R_Address,R_Phone FROM Request_Send req_snd INNER JOIN Blood_Request bld_req ON bld_req.RequestID=req_snd.RequestID INNER JOIN Donor dnr ON req_snd.DonorID=dnr.ID Where D_Emial='" + Session["UserID"] + "'";
please make sure D.Emial column is there in the table and call it like : tablename.D_Emial.

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.

mySQL SELECT from one table, JOIN from another, then SELECT from the new one

I have an recruiting database that looks like this: Recruiter has many Students. Students have many "contacts" (how many times we have called/emailed etc.)
I want a recruiter to be able to see when the last time each student of theirs was contacted. I'm developing this app in c# and my database is mySQL.
My table names are employee, students, contact_his and the fields I want to join, given one employeeid are employee.idemployee + students.employee_id then join all of those to contact_his.students_id + students.idstudents. But I have no idea how joins work.. My current code looks like this, but it does not like it:
"SELECT students.* FROM admissions.students
WHERE students.employee_id='PASS VARIABLE HERE'
JOIN contact_his ON contact_his.students_id = s.idstudents
WHERE c.date = (SELECT MAX(date);"
SELECT s.*, MAX(h.date) last_contact
FROM students s
JOIN contact_his h ON h.students_id = s.idstudents
WHERE s.employee_id = 'PASS VARIABLE HERE'
GROUP BY s.idstudents

Categories

Resources