i have Two Table Like This
Request Example UniTerm Example
St_Code 1 Id 1 2
St_Name FirstStudent Term 96-97 95-94
St_Family FirstFamily
and i have a Grid View contain St_Code,Name,Family and Term
now the problem is i want to display all records of the student with the Just Last Record Term field in Term table
i need gridview data like below
1 FirstStudent FirstFamily 96-97
show last record of the Term table and all of the Student table Records
i tried this SQL Code to select them
SELECT Request.St_Code, Request.St_Name, Request.St_Family, UniTerm.Term FROM Student CROSS JOIN UniTerm ORDER BY UniTerm.Term DESC
its ok but shows all of the records in UniTerm table i need just last one!
thanks in advance
Just update your query
SELECT Request.St_Code, Request.St_Name, Request.St_Family, UniTerm.Term FROM Student CROSS JOIN UniTerm ORDER BY UniTerm.Id DESC
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...
How to show or retrieve multiple columns with distinct value from one table in sql query ?eg: in my database i have account id 13 and id 13 have balance of 8000 and remaining fields are different but id and balance is same in 9 records, so i want id and balance to display only once and for remaining fields it should display 9 records
Try like this;
SELECT DISTINCT c1,c2,c3 FROM table
Or you can use group by
SELECT c1,c2,c3 FROM table group by c1,c2,c3
May be the Question's title is not correctly defined what i actually wanted to ask. Here is the more specific description of my question
I have a following table User in my database which has a column i.e. Category which contains multiple values but separated by commas
S.no. Name Category
1 Ankit Ex Soldier, Senior Citizen
2 Ritu Widow, Senior Citizen
3 Akash Ex soldier
I wanted to search the record on the basis of category
for eg. If i search
select * from User where Category='Senior Citizen'
Then it must show Ankit and Ritu record.
How to do this.
plz help
try this:
select * from User where Category like '%Senior Citizen%'
You need LIKE operator:-
select * from User where Category LIKE '%Senior Citizen%'
select * from User where Category LIKE '%Senior Citizen%'
But you should use a separate table for Category.
Like Kiss László wrote you should separate the information in two tables. The professional term for this is called "Normalization". Most important to know are the 1NF, 2NF and 3NF (read this for detailed information).
So it should look like the following:
Table Persons
PersonId Name
1 Ankit
2 Ritu
3 Akash
Table Categories
CategoryId Name
1 Ex. Soldier
2 Senior Citizen
3 Widow
Table PersonCategories
PersonId CategoryId
1 1
1 2
2 2
2 3
3 3
Why should you do this?
In my opinion the biggest reason is performance. I made some test table with your current approach with a data set of 20k entries. The execution of the query took about ~200ms to return. With the schema above the following query executed in about ~1ms
SELECT
*
FROM
Persons AS p
JOIN
PersonCategories AS pc ON p.PersonId = pc.PersonId
JOIN
Categories AS c on pc.CategoryId = c.CategoryId
WHERE
c.Name = 'Senior Citizen'
Why is this query so much faster?
Because we can easily use indices on our columns. In the schema above the Persons.PersonId and Categories.CategoryId are the PRIMARY KEY columns of their tables. So to use them as a column for a JOIN operation has minimal costs. Both columns of the PersonCategories table are FOREIGN KEYS (ensures a valid database state and improves performance). Finally the Categories.Name column has an INDEX too.
Could this approach be bad?
In most cases this is the way to go. One reason not to do it this way, is if you have to handle lots of INSERTS. INSERTS in this schema have a much higher cost because all indices need to be updated after the INSERTS.
I have student table in SQL Server.
In my web application I have textBox, where I can input a date (dd-mm-yyyy). This date means that any(application know which exactly) student was absent this day.
How to save this date in SQL Server?
There is no problem when student is absent just one day per all life, because I can create one additional column in my student table and save there date of absent.
But I don't know how many days student will absent. I can add thousand columns to my student table and write in there dates of absents, but it's very bad solution.
So, how to save dates of absence in SQL?
I wrote my web application in ASP.NET, C#, SQL Server.
You need to have another table to keep track of the dates the student was absent.
Say your current table is as follows:
Student
-------
StudentId [PK]
...
So you would now create another table as:
StudentAbsent
-------
StudentAbsentId [PK]
StudentId [FK, Student.StudentId]
AbsentDate
To get the dates the student with id=5 was absent you'd do something like the following in SQL
SELECT AbsentDate FROM StudentAbsent
WHERE StudentId = 5
Oh and BTW you'd want to read more on relationships. If it's a 1-1 relationship one row of table1 is related to one row of table2.
If it's a 1-n relationship, one row of table1 is related to many rows of table2 (as is the case above)
If it's a n-n relationship, one row of table1 is related to many rows of table2 and vice-versa.
You have to create another table called absents with three columns:
id (primary index and auto_increment)
student_id (should not be unique)
date
The id column is just the id of the absent (it's a good practice to have id for every row on a table). The student_id is a reference to id column of students table, identifying the correct student. And the date column is the date of absent.
Another good practice is to create relationship internally and set triggers to actions like delete (what should you do if the student is deleted?).
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/