I have table Student with a column called IdStudent.
The value of IdStudent is 0
Table Student also have a column called UID
I need to update IdStudent in table Student with IdCandidate in table Candidate.
Table Candidate also have UID column containing the same UID of table Student.
So we can do this to have IdCandidate:
select C.IdCandidate from Candidate as C inner join Student as S
on C.UID = S.UID
How can I update IdStudent in table Student with this IdCandidate obtained in this select?
Thanks!
Use JOIN in update
update s set s.IdStudent = C.IdCandidate
from Candidate as C
inner join Student as S on C.UID = S.UID
You can do it using the following query :
update S set S.IdStudent = C.IdCandidate
from Student S
inner join Candidate C on S.UID=C.UID
Related
I have a Students table with 2 col as Rollno and Marks with 12 records
I added another column in students table as Name. How do I fill Names with 12 records from another table in SQL Server?
I tried this:
SELECT * FROM [SampleDB].[dbo].[Student_SQL]
insert into Student_SQL(name)
select name
FROM [School].[dbo].[StudentMaster]
update [Student_SQL]
set name = 'David'
where RollNo = 4`
I think you want the upate/join syntax. Assuming that the two tables relate through column rollno:
update ss
set ss.name = sm.name
from student_sql ss
inner join student_master sm on sm.rollno = ss.rollno
The upside of this approach is that it filters the rows and only update those that match in the master table.
in order to do this, you need to have column in common for both tables
update [Student_SQL] s
set name = (select name from other_table o where o.roll_no = s.roll_no)
I need some help with a SQL query. I have a table of courses and a table that contains user id and course id, denoting courses that user has taken (might not have taken any; no entry in that table for that user id).
I need a query to return the list of courses not taken.
Course Category table
CategoryID
Caegory
Courses table
CourseID
CategoryID
CourseName
...
UserCourse table
UserID
CourseID
you can use not exists
Select *
From Courses c
Where Not Exists (Select 1 From UserCourse uc Where uc.CourseID = c.CourseID)
This will just list the course
select *
from Courses C
Left join CourseCategory cc on
cc.CategoryID = c.CategoryID
where CourseID not in (Select CourseID from UserCourse where UserID = 14)
what i need is for a given user id and course category, what courses within that category have not been taken by this user
(This should have been in the request by the way.)
So:
Select from courses.
Limit to the desired category.
Limit to courses not in the set of courses taken by the user.
The query:
select *
from courses
where categoryid = 123
and courseid not in (select courseid from usercourse where userid = 456);
Another way of writing same query, which will perform faster.
select C.CourseID,C.CategoryID
from Courses C
Left join CourseCategory cc on
cc.CategoryID = c.CategoryID
left join UserCourse uc
on C.CourseID=uc.CourseID
where uc.CourseID is null
I have one table name Class_sub where I have subjects name according to the classes, I have weekly_test table where the same subject's marks have to be entered.
My problem is to show subject name as field name in weekly_test table reference to class_sub table data fields.
If you have the following tables:
CLASS
id
name
SUBJECT
id
name
CLASS_SUBJECT
id
class_id
subject_id
WEEKLY_MARKS
id
class_subject_id
test_date
mark
The following query will work:
SELECT s.name as subject_name,
c.name as class_name,
wm.test_date,
wm.mark
FROM weekly_marks wm
INNER JOIN class_subject cs ON wm.class_subject_id = cs.id
INNER JOIN class c ON cs.class_id = c.id
INNER JOIN subject s ON cs.subject_id = s.id
A bit of a guess here, but assuming the class_subject table you're talking about is joining of subjects (students?) to the classes they are enrolled in.
I am using Postgresql in my company as a primary store and I'm struggling to implement an update query involving three different tables.
Here is the schema :
Table 1 has entity_id, entity_type and company_id (referencing Company.id) columns.
Company has id column
Special_company has id (which corresponds to Table1.entity_id) and company_id (referencing Company.id)
I would like to update all the rows of Table 1 where Table1.entity_type = 'SpecialCompany' in order to fill Table1.company_id such that :
Table1.entity_id = SpecialCompany.id and SpecialCompany.company_id = Company.id
I've started something like that:
UPDATE Table1
SET company_id = (select c.id
FROM company c
INNER JOIN special_company w ON c.id=w.company_id
WHERE w.id=709)
WHERE entity_type='SpecialCompany' AND entity_id=709;
But I am not able to replace 709 by all the Table1.entity_id where Table1.entity_type = 'SpecialCompany'.
Any help would be much appreciated.
I assume you are looking for something like this.
update t
set t.company_id=c.id
from
Company C
INNER JOIN special_company w ON c.id=w.company_id
INNER JOIN Table1 t on t.entity_id=w.entity_id
Where t.entity_type = 'SpecialCompany'
I'm answering to my question as I found the solution :
UPDATE Table1 t
SET company_id = c.id
FROM Company c
INNER JOIN special_company w ON c.id=w.company_id
WHERE
t.entity_type = 'SpecialCompany' AND t.entity_id=w.id;
Thanks #Krishna for your help !
I have 3 tables
Student (studentID, TeacherID)
Teacher (TeacherID, Number)
a temp table StudentUpdate (StudentID, TeacherNumber)
How do I update the student.TeacherID with values from studentupdate.teacherNumber?
Please see the difference between teacherID and teacher Number. One is the PK and one is just a nvarchar column. Thanks in advance.
You can do that using below query. Assuming you have only one teacher for each student
update st
set st.TeacherID = t.TeacherID
from Student st
inner join StudentUpdate su on su.StudentID = st.StudentID
inner join Teacher t on t.Number = su.TeacherNumber