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.
Related
I have a gridview in my web project and I connect it with the SqlDataSource. The select command contain fields from 3 table, and I created commandField for gridview so that users can delete their Purchase Items.
here is my C# select code:
SqlDataSource1.SelectCommand =
"SELECT Production.Name, Production.Price ,PurchaseItem.Quantity,
Production.Info ,Purchase.OrderDate
FROM Production
INNER JOIN PurchaseItem on (Production.ID = PurchaseItem.ProductID)
INNER JOIN Purchase on(Purchase.ID = PurchaseItem.PurchaseID)
WHERE Purchase.UserID = '" + Convert.ToInt64(da.getuserid(Session["Mysys"].ToString()).FirstOrDefault()) + "'";
GridView1.DataSourceID = SqlDataSource1.ID;
GridView1.DataBind();
My problem is the DeleteCommand of the SqlDataSource.
is it Possible to do such thing?
Production, PurchaseItem, Purchase are the tables you want to delete the items by selecting on your gridview right? if so.
you just have to make a delete query like
DELETE FROM PRODUCTION WHERE ID = VALUE
DELETE FROM PURCHASEITEM WHERE ID = VALUE
DELETE FROM PURCHASE WHERE ID = VALUE
and if you want to delete running a single query only you may try INNER JOIN:
DELETE table1, table2 FROM table1 INNER JOIN table2
WHERE table1.id=table2.id and table1.id = 'VALUE'
I am working on a project.I have a table tblhomework which have a column students code-which contains students code separated by comma.Now I have few drop downs to search a record among which is search by student name.Now I have students code in my table how should I match it against the student name.I am using the following query,till now I am matching using students code but now I have to match by student name
Query
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
SELECT
tblhomework.ID,
tblteacher.TEACHERNAME,
tblclass.CLASSNAME,
tblhomework.Title,
tblhomework.HomeworkDetail,
tblhomework.StudentsCode
FROM tblhomework
join tblclass on tblclass.CLASSCODE=tblhomework.ClassCode
join tblteacher on tblteacher.TSHORTNAME=tblhomework.Tshortcode
where tblhomework.StudentsCode like'" + studentcode + "';
Following is the screenshot of the result I am getting
I have a student table which contains student names of students which you can use.
Using FIND_IN_SET in your current SQL:-
SELECT
tblhomework.ID,
tblteacher.TEACHERNAME,
tblclass.CLASSNAME,
tblhomework.Title,
tblhomework.HomeworkDetail,
tblhomework.StudentsCode
FROM tblhomework
JOIN tblclass ON tblclass.CLASSCODE=tblhomework.ClassCode
JOIN tblteacher ON tblteacher.TSHORTNAME=tblhomework.Tshortcode
WHERE FIND_IN_SET('" + studentcode + "', tblhomework.StudentsCode);
However, not this will only work if there are no spaces before / after the commas in the list of student codes
To join that against the student names table (and making assumptions on the layout of that table) use something like this:-
SELECT
tblhomework.ID,
tblteacher.TEACHERNAME,
tblclass.CLASSNAME,
tblhomework.Title,
tblhomework.HomeworkDetail,
tblhomework.StudentsCode ,
tblstudent.StudentsName
FROM tblstudent
JOIN tblhomework ON FIND_IN_SET(tblstudent.StudentsCode, tblhomework.StudentsCode);
JOIN tblclass ON tblclass.CLASSCODE=tblhomework.ClassCode
JOIN tblteacher ON tblteacher.TSHORTNAME=tblhomework.Tshortcode
WHERE tblstudent.StudentsCode = '" + studentcode + "'
Searching by student name (although generally it would be easier if the drop down returned the numeric unique student id):-
SELECT
tblhomework.ID,
tblteacher.TEACHERNAME,
tblclass.CLASSNAME,
tblhomework.Title,
tblhomework.HomeworkDetail,
tblhomework.StudentsCode ,
tblstudent.StudentsName
FROM tblstudent
JOIN tblhomework ON FIND_IN_SET(tblstudent.StudentsCode, tblhomework.StudentsCode);
JOIN tblclass ON tblclass.CLASSCODE=tblhomework.ClassCode
JOIN tblteacher ON tblteacher.TSHORTNAME=tblhomework.Tshortcode
WHERE tblstudent.StudentsName = '" + studentname + "'
You can use 'FIND_IN_SET' function to search in comma separated set of values.
Change your where clause as below:
where FIND_IN_SET( #student_code, tblhomework.studentscode );
Use AddWithValue to bind value of 'studentcode' read from text box.
string studentcode = txt_studentcode.Text.ToString();
myCommand = new MySqlCommand( sql_query_string, connection );
myCommand.Parameters.AddWithValue( "#student_code", studentcode );
And execute.
To store comma separated values in column is bad design you should look at Database Normalization and do normalize your structure by storing all related student codes in a junction table,but as from above comments you said you can't change structure so in mysql you can use FIND_IN_SET() and join your students table ,i am not familiar with c# syntax so i have posted mysql query,also use your original name convention for the columns
SELECT
DISTINCT
h.ID,
t.TEACHERNAME,
c.CLASSNAME,
h.Title,
h.HomeworkDetail,
h.StudentsCode
FROM
tblhomework h
JOIN tblclass c
ON c.CLASSCODE = h.ClassCode
JOIN tblteacher t
ON t.TSHORTNAME = h.Tshortcode
JOIN students s ON(FIND_IN_SET(s.StudentsCode, h.StudentsCode) > 0)
WHERE FIND_IN_SET('studentcode', h.StudentsCode) > 0
OR s.student_name LIKE 'test'
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.
I have a datebase and a DataGridView that i bind by the request SELECT idUser, nameUser, idFunction FROM dbo.Users.
table_Users {idUser, nameUser, idFunction}
table_Functions {idFunction, nameFunction}
How can I replace the colum "idFunction" of the grid view by "nameFunction"?
You want to use a join linking the two tables based on idFunction
SELECT idUser, nameUser, f.nameFunction FROM dbo.Users u
Inner Join table_Functions f on f.idFunction = u.idFunction
More information on joins can be found here
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