Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Update Using Multiple Tables [duplicate]

Tags:

sql

sql-server

I don't know much about SQL - I'm just writing an RFID student registration scanner that interfaces to a database in SQL Server.

I'm sure this is quite easy to do but I could not find a solution for my problem.

I want to be able to do something like this in a basic form which won't work:

UPDATE Attendance 
SET    A1 = 'TRUE' 
WHERE  Student.ID = '3a0070c48' 
  AND  Module.Day = 'Friday' ;

But the full SQL Update needs to be something like this:

UPDATE Attendance 
SET    A1 = 'TRUE' 
WHERE  Student.ID = '3a0070c48' 
  AND  Module.Day = 'Friday' 
  AND  '1100' BETWEEN Module.StartTime 
                  AND Module.EndTime ;
like image 229
Richard Pearce Avatar asked Mar 21 '13 18:03

Richard Pearce


1 Answers

Ok, you need to do something like this:

UPDATE A
SET A.A1 = 'TRUE' 
FROM Attendance A
INNER JOIN Student S
    ON A.StudentId = S.ID
INNER JOIN Module M
    ON A.ModuleId = M.ID
WHERE S.ID = '3a0070c48' 
AND M.[Day] = 'Friday' 
AND '1100' BETWEEN M.StartTime AND M.EndTime

I guessed the columns that related your tables, but it should be very close to what you have, you need to use the real columns.

like image 187
Lamak Avatar answered Sep 22 '22 04:09

Lamak