I have two tables..
Students
StudentID
Name
SchoolID
School
SchoolID
SchoolName.
This is my scenario, I have created a DetailsView on my page which displays X StudentID and its columns. When displaying the SchoolID (from Students table) column it will display the actual ID as its supposed to.
I would like to know how I can display the actual school name instead of the schoolID, what I did first was simply have a query inside the first query which displays the name of the school like...
SELECT StudentID, (SELECT SchoolName FROM Schools
WHERE Schools.SchoolID = Students.SchoolID)
FROM Students WHERE StudentID=1
Although this works this creates issues when trying to edit the record via GridView, it will not save the correct data in those fields.
In the edit step in GridView I'm able to bind the SchoolID key with SchoolName, however this will NOT work when having a query within a query.
So my question really is how can I display foreign key data within Visual Studio without changing the SQL command?
The solution previously was to edit the Select command (after the SQLDatasource was created) to perform a inner join on the table containing the meaningful field name.
Check Here
1.Link
2.Link
You could do that with join query, example:
select students.StudentId,students.Name,students.SchoolID,school.SchoolName from students join school on students.SchoolID=school.SchoolID
And, for better understanding of "JOINS" here is useful link: http://www.sitepoint.com/understanding-sql-joins-mysql-database/
Related
In my attendance table I want to update a record with S_ID and C_ID from StudentCourse bridge table but its not updating.
https://imgur.com/a/7ZBItur
Its working when I use it to select and display 2 columns from the StudentCourse table and 1 column from the Attendance table but it doesn't work when I use it to update the attendance table which for now is empty
UPDATE Attendance
SET S_ID = sc.S_ID,
C_ID = sc.C_ID
FROM Attendance a
left outer join StudentCourse sc ON a.S_ID = sc.S_ID
WHERE sc.S_ID=2 and sc.C_ID=2
There are two actually four tables:
Student(S_ID(primary key))
Course(C_ID(Primary key))
StudentCourse((S_ID,C_ID(Foreign keys))Bridge table)
and attendance table with (S_ID,C_ID(Foreign keys))
What I am doing is displaying data on datagridview by joining tables and selecting S_ID,S_Name,C_ID and Pre_Abs (Attendance table column)columns from all these tables.
Now, I want to insert the info present in datagridview to the attendance table when I click on button.
I have done this already with simple insert query to attendance table by using datagrdview.rows[i].cell[2] property.
I want to know if there is any better idea to do this so that I can use JOIN instead of using datagridview property with for loop.
For now my attendance table is empty while Student, Course, and StudentCourse tables are filled with the data.
What I want is to display record(S_ID, C_ID) from studentCourse table and (Pre_Abs) from Attendance table and when I submit the attendance; I want it to store Pre_abs record against each S_ID, C_ID in the attendance table.
I don't think I can explain it any further.
You have declared alias for the table Attendance. So you should use alias reference before the column name as the same column name also available in other table. Can you please try this-
UPDATE a
SET a.S_ID = sc.S_ID,
a.C_ID = sc.C_ID
FROM Attendance a
LEFT OUTER JOIN StudentCourse sc ON a.S_ID = sc.S_ID
WHERE sc.S_ID=2 and sc.C_ID=2
I have also doubt about you where condition as you are Update table Attendance but filtering records on table StudentCourse. use of wrong filter can resultant in update all rows in table Attendance. Please be careful.
Hmmm. I do not understand the reason why your Attendance table exists in the first place. Your StudentCourse table already seems to contain info about which students are having which courses. I'll just assume the Attendance table has to deal with registering student attendance for each single lecture/lesson within a course.
Anyway, as #Psi already commented, you seem to initially want an INSERT query to create a record in the Attendance table with data based on information in the StudentCourse table. I guess that you are thinking about it too hard. ;-) You may try something like this:
INSERT INTO Attendance (S_ID, C_ID, Pre_Abs)
SELECT S_ID, C_ID, Pre_Abs
FROM StudentCourse
WHERE S_ID = 2 AND C_ID = 2
However, it is currently unclear how field Pre_Abs should be filled... You try to get it from table Attendance, but that would seem to be invalid.
Also, when creating INSERT-queries, make sure that the source data values (in the SELECT or VALUES clause) are in the same order as the target fields (in the INSERT clause). If the field/value order doesn't match, the data gets mixed up (if the query does not fail due to data type errors)!
And last but not least: if both Attendance and StudentCourse tables share the same composite key (S_ID, C_ID), you should use that full key when joining related records of those tables:
FROM
Attendance AS A
LEFT JOIN StudentCourse AS SC ON
SC.S_ID = A.S_ID AND
SC.C_ID = A.C_ID
Hope this helps a little...
I have sql like:
select employee_name, employee_id from employees;
select admin_name, admin_id from admin;
Yep! we all know how to populate an sql query in a dropdown list but what I am looking for is on how would I possibly populate the data of this two sql in a single dropdown ?
Does anyone have a magical idea ? BTw, I'm using c# asp.net and even open to use jquery, json or the like to achieve this.
You can use JOIN to combine these 2 queries.The number of columns and datatype of corresponding columns in each queries must be same.
select employee_name as name, employee_id as id from employees
UNION
select admin_name as name, admin_id as id from admin
you can use 'name' as textfield and 'id' as value field
If you are trying to create dropdown data using this query,there may be a problem of identifying admin and employees seperately(both may have same id)
you can try this
To get both table data in single query, you should put union between both tables. Column names from both tables should same also. For that you can use column aliasing.
(select employee_name as name, employee_id as id from employees)
Union all
(select admin_name as name, admin_id as id from admin);
You can bind data from this query to your drop down list.
Hope it work for you.
I am building a windows form C# app. and I use oleDb for linking access database to my app. the problem is, My access database has two tables (students,courseCodes) and one column of the "students" table(courseName) is linked to one in the "courseCode" table (the "courseCode" table contains course codes for example course code 1 is Static and I use code 1 in the "students" table for displaying Statics) now when I want to select column containing Statics using
"SELECT DISTINCT courseName FROM students";
I got the "1" instead "Statics" is there any way to retrieve "Statics" instead "1"?
I'd say your naming convention is misleading and confusing. The column should be courseIndex, not courseName.
Do a JOIN, of course (no pun intended). This query will return the distinct course names that a given student has signed up for.
select distinct courseCode.courseName
from student
join courseCode
on student.courseId = courseCode.id
where student.id = ?
Please adjust for your schema details.
Personally I think this is a poor design. A student can sign up for many courses, and a course can have many students. This is a many-to-many relationship. You need a join table; sounds like you only have a foreign key one-to-many relationship here.
Hello I am making a reservation system and I am having troubles with my table can someone help me with my problem.
I have a table customer and a table reservation.
My customer table handles all the customers personal info and the reservation handles the reservation details.
My customer table has a customer ID which is a primary key and auto increment. and
my reservation table has a reservation ID which is a primary key and auto increment.
MY PROBLEM IS... How could I connect the two?
I have a big problem on how to join them in my select statement because I dont know what values will I join.
*Note: Btw, I'm using c# winform and I seperated the add customer and add reservation details. I am also wondering if I can include the 2 insert statement in one button or add them seperately..
Use the SQL JOIN statement in your SELECT QUERY:
SELECT c.CustomerName, r.ReservationTime
FROM reservation r
JOIN customer c ON r.CustomerId = c.CustomerId
Edit: It sounds like you are having trouble understanding how to get a newly created CustomerId in order to include it in the Reservation table. The key here is LAST_INSERT_ID(). After your insert into Customer, get the value from LAST_INSERT_ID() - that is the newly created CustomerId. You can then use that value when you insert into Reservation. Eg:
INSERT INTO Customer (CustomerName)
VALUES ('Joe Schmoe');
INSERT INTO Reservation (ReservationTime, CustomerId)
VALUES ('2011-09-12 18:30:00', LAST_INSERT_ID());
Pardon syntax errors - SQL Server is my primary language, if you hadn't gathered that already.
Well, your reservation table should have a customer ID as well.
As I understand you, that's not the case yet.
But it should be like that, because every reservation belongs to a customer.
Then, you can join both tables on the customer ID.
I have a local report that I created in my application. It displays the contents of a table in my access database called "History". One of the columns is "ID" and it refers to the primary key in another table "Employees".
My report shows the employee's ID when it is refreshed but of course I want it to display the employee's name instead.
In short, I need to be able to perform a lookup on another table in my rldc file. How can I go about that?
I assume you fetch data from the database through a dataadapter and a typed dataset. something like this?
TestDataSetTableAdapters.CategoryTableAdapter ca = new TestDataSetTableAdapters.CategoryTableAdapter();
this.ds1 = new TestDataSet();
ca.Fill(this.ds1.Category);
Then you go to the dataset and modify the query in your tableadapter to something like this
select h.*,e.EmployeeName from history h
inner join Employees e on e.ID = h.UserID
Then the datset will be modified to include the column EmployeeName on all rows in the History table. Then the column "employeename" is directly available in yyour report
Here is an example of how to join your employee and history tables with SQL. My Access is rusty, so the syntax might not be perfect, but this should demonstrate the concept. Obviously, I made up the column names, so you will have to change the columns to whatever you need.
SELECT EmployeeID,
EmployeeName,
TimeIn,
TimeOut
FROM Employee Emp
INNER JOIN History History
ON Emp.EmployeeID = History.EmployeeID