Update Table Column From Another Table? - sql

So I have two tables. Say one table has a list of students, a student ID for each student, and a home address for each student.
Then you have another table that has a subset of the students in the first able, (and they are in a completely different order) with updated addresses and a student ID.
I need a query that can match the student ID of the two tables, and thereby update the address from the first table, using what is in the second table.
This is the query I tried, but no luck:
UPDATE Roster, UpdatedRoster
SET Roster.Address = (SELECT Address FROM UpdatedRoster WHERE Roster.StudentID = UpdatedRoster.StudentiD)
WHERE Roster.StudentID = UpdatedRoster.StudentiD
Any help here would be greatly appreciated.
Update: This is on Microsoft Access FWIW.

UPDATE Roster
SET Roster.Address = UpdatedRoster.Address
FROM Roster, UpdatedRoster
WHERE Roster.StudentID = UpdatedRoster.StudentiD

Related

How to query in a way that relates a name to an ID and then using that ID to sum up what comes under that ID

I'm a student learning SQL. With querying, if you are given data that you have to relate to a primary key in the same table that will then be used to identify data in another table, how do you query that? I only want a push in the right direction because I don't want to copy and paste someone's code.
For example:
You are to find out how many staff are being managed by a manager and you are to find this by using the managers first and last name (which is not the primary key). The database is below. ManagerNo and StaffID are the same.
Branch (BranchNo, ManagerNo)
Staff (staffID, fName, lName, position, BranchNo)
Thank you for your time
You would use two joins:
select s.*
from staff s join
branch b
on s.BranchNo = b.BranchNo join
staff sm
on sm.staffId = b.ManagerNo
where sm.fname = ? and sm.lname = ?;

How to Update missing data in an MS Access table based on data from that same table?

I have a table of health plan subscribers we'll call it table1. Members are listed multiple times as all of their benefits are listed (Plan Name field). In some cases they may have an address listed for one Plan Name and not for the other. I'm blanking on how to create an update query that will fill in the Street Address 1, Street Address 2, City, State, and Zip Code based on the same fields that they have listed for another Plan Name in the same table? I can identify the members by their Member_ID
Using most of the comments and suggestions above:
If you link the table to itself:
SELECT Parent.*, table1.PlanName AS Plan FROM table1 AS Parent
LEFT JOIN table1 on Parent.Member_ID = table1.Member_ID
WHERE Parent.zipcode is not null
This should get what you have asked for!

Postgres Update multiple rows

I need to update multiple rows in a table.
i have table customer and contact
I need to update customer where the linked contact in the contact table has the column city at a certain value.
I can get the rows i need with this query
Select cus.id, con.city
from customer cus, contact con
where cus.contacts_id=con.id
and con.city="MyValue"
I know how to update one table but do not understand how to update the table when the rows are looked up from a different table.
Firstly, please do not use the old JOINs (FROM comma separated tables).
Secondly, here you go:
UPDATE customer SET whatever = 'whatever value'
WHERE contacts_id IN (
SELECT id FROM contact WHERE city="MyValue"
)

Extract info from one table based on data from antoher

I am kind of new to SQL and I made a couple of tables to practice. The columns may have some unrelated categories but I don't know what else write...
Anyway, basically what i want to do is get info from two tables based on the first and last name from one table.
Here are my tables:
Order
Host
I want create a query to pull the ticket number, height, order, subtotal and total by first and last name. The only orders I want to pull are from John Smith And Sam Ting. So in the end, I want my extraction to have the following columns:
Ticket Number
First Name
Last Name
Height
Order
Subtotal
Total
Any help or direction would be awesome!
With the assumption the tables both have unique Ticket_Numbers and that will provide a one-to-one mapping between then.
SELECT
Order.Ticket_Number,
First_Name,
Last_Name,
Height,
Order,
Subtotal,
Total
FROM Order
JOIN Host on Host.Ticket_Number = Order.Ticket_Number
WHERE
(First_Name = 'John' AND Last_Name = 'Smith')
OR (First_Name = 'Sam' AND Last_Name = 'Ting')
You need to "call" the table name first, and then the column. After that you need to use the "join" for the 2 tables. And finally you need the "where". I didn't look for the details so you need to check the "names".
SELECT Order.Ticket_Number, Order.First_Name, Order.Last_Name, Order.Height, Order.Order, Cost.Subtotal, Cost.Total
FROM Order
INNER JOIN Cost
where First_Name="Jhon" and Last_Name="blablabla"
or
First_Name="SecondGuy" and Last_Name="blablabla"

SQL query where data from one table = data from another

I have 2 tables: person_concern and person. They both have a code column and person has a dept column. I want to select data from person_concern table where the dept column in person table = 30.
Basically the person table has a different code for each row and then the person is put in a department. So i can have multiple rows in the person table with the same dept field.
The person_concern table is for writing problem concerns for people. It has a code to know which person its referring to which id get from the person table. So i want to select data from that person_concern table where the code matches the code from the person table and that person is from a certain dept such as 30.
Hope that makes sense... Here's what ive tried so far but get an invalid number error.
select
PERSON_CONCERNS.CODE
PERSON_CONCERNS.ENTRY_DATE
PERSON_CONCERNS.ENTRY_OPR
PERSON_CONCERNS.DISCUSSION
from PERSON_CONCERNS
inner join PERSON on PERSON_CONCERNS.CODE = PERSON.CODE
where PERSON.DEPT = 30
I think you are just missing the commas on your field names but need to see the actual code you are running along with the create table statements and some sample data to be sure.
select
PERSON_CONCERNS.CODE,
PERSON_CONCERNS.ENTRY_DATE,
PERSON_CONCERNS.ENTRY_OPR,
PERSON_CONCERNS.DISCUSSION,
from PERSON_CONCERNS
inner join PERSON on PERSON_CONCERNS.CODE = PERSON.CODE
where PERSON.DEPT = 30