I have these tables:
Users
Id (PK)
NationalCode
UserProfiles
UserProfileId (PK) One to One with Users
SalaryAmount
Salaries
NationalCode
SalaryAmount
I want to update SalaryAmount for each user inside UserProfiles with new one in Salaries. How can I do that?
I have tried this:
UPDATE Users
SET SalaryAmount = t2.Salary
FROM Users t1
INNER JOIN Salaries t2 ON t1.NationalCode = t2.NationalCode
GO
The above code works if SalaryAmount be inside Users, But as you can see SalaryAmount is inside UserProfiles.
All you need is another join on `UserProfiles:
UPDATE up
SET up.SalaryAmount = s.Salary
FROM UsersProfiles up
JOIN Users u on up.UserProfileId = u.Id
JOIN Salaries s ON u.NationalCode = s.NationalCode
UPDATE up
SET up.SalaryAmount = t2.Salary
FROM UserProfiles up
INNER JOIN Users t1 ON t1.Id = up.UserProfileId
INNER JOIN Salaries t2 ON t1.NationalCode = t2.NationalCode
GO
Is that what you need? I would recommend you to make UserProfileId column to be unique in table UserProfiles and store Users's unique Id in Column UserId which is a Foreign Key for table Users
UPDATE U
SET U.SalaryAmount = S.SalaryAmount
FROM
Userprofiles U
INNER JOIN Users R ON U.UserprofileID = R.ID
INNER JOIN Salaries S ON R.NationalCode = S.NationalCode
WHERE
U.SalaryAmount <> S.SalaryAmount
Related
I have 2 tables Users and Orders. Primary key of Users is UserId, which I have defined as foreign key in the Orders table. I have added a new column of UserName to Orders. I want to import UserName from Users table.
I'm trying to run this code, but it keeps giving me errors. Can someone tell me what could be wrong?
UPDATE Orders
SET Orders.UserName = Users.UserName
INNER JOIN Users ON Orders.UserId = Users.UserId;
That's not the right syntax
UPDATE O
SET O.UserName = U.UserName
from Orders O
INNER JOIN Users U ON O.UserId = U.UserId ;
You need to start using Alias for tables references when there is more than one table involved in your query. Using Alias names makes the query more readable.
you are missing orders table name
UPDATE Orders
SET Orders.UserName = Users.UserName
from Orders
INNER JOIN Users ON Orders.UserId = Users.UserId ;
No UPDATE with INNER JOIN, please re-write your query as follow:
You can use INNER JOIN only after FROM clause
UPDATE Orders SET Orders.UserName = Users.UserName
FROM Users WHERE Orders.UserId = Users.UserId
I actually like the CTE version of the update here, because it lets us expression the join logic as we would in a select, without any potential confusion:
WITH cte AS (
SELECT o.UserName AS user_old, u.UserName AS user_new
FROM Orders o
INNER JOIN Users u ON u.UserId = o.UserId
)
UPDATE cte
SET user_old = user_new;
Your query structure should be like below :
UPDATE
t1
SET
t1.c1 = t2.c2,
t2.c2 = expression,
...
FROM
t1
[INNER | LEFT] JOIN t2 ON join_predicate
WHERE
where_predicate;
So your actual query should be like below :
UPDATE Orders SET Orders.UserName = Users.UserName
FROM Users WHERE Orders.UserId = Users.UserId
I am aware that I am asking something that may look trivial but I could not find and answer or a duplicate. (I am new with this area).
So i am trying.
I have two tables
Tests table with the following columns
1. storyID , authorID , assigneeID
2. userID , userName
I would like to write a query that return storyID, UserName of author ID, UserName of assigneeID.
SELECT storyID, u1.userName AS authorName, u2.userName AS assigneeName
FROM storytable s
INNER JOIN usertable u1 ON s.authorID = u1.userID
INNER JOIN usertable u2 ON s.assigneeID = u2.userID
The above solution will only work in case you have relation between userID, assigneeID and authorID fields.
Select and create an alias for the second table.
SELECT a.storyID, b.userName, c.userName
FROM tablea a
INNER JOIN tableb b ON a.authorID = b.userID
INNER JOIN tableb c ON a.assigneeID= c.userID
Assuming the authorID = UserId...
select
1.storyId,
author.userName,
assignee.userName
from 1
inner join 2 as author
on 1.authorId = 2.userId
inner join 2 as assignee
on 1.assigneeID = 2.userID
These are my tables:
Customer Table
--------------
CUST_ID
SUPPLIER1
SUPPLIER2
Supplier Table
--------------
SUPPLIER_ID
USERID
User Table
----------
USER_ID
The rules:
Customers can have multiple suppliers but suppliers MUST be in the user table if they have a customer.
Because all suppliers have to be users, I need to find all used or assigned suppliers that are missing from the user table.
This query gets me all suppliers that are not users
SELECT
U.*
FROM
USER U
LEFT JOIN
SUPPLIER S ON S.USER_ID = U.USER_ID
WHERE
S.USER_ID IS NULL
But how do I get all of the customer's suppliers that are not users?
I tried this but it doesn't seem to be correct:
SELECT
*
FROM
USER U
LEFT JOIN
(SELECT C.SUPPLIER1, S.USER_ID FROM CUSTOMER C, SUPPLIER S WHERE
C.SUPPLIER1 = S.SUPPLIER_ID AND C.SUPPLIER1 IS NOT NULL)
S2 ON S2.USER_ID = U.USER_ID
WHERE
S2.USER_ID IS NULL
Thanks
SELECT
C.Supplier1
FROM Customer C
LEFT JOIN Supplier S
ON C.Supplier1 = S.supplier_id
WHERE S.user_id IS NULL
Is your problem that the Customer table is not normalized, i.e., you have repeated supplier columns in the customer table. You can join the suppliers table twice. e.g.
select C.Supplier1, U1.UserID, C1.Supplier2, U2.UserID
from Customer C
left join Supplier S1 on (S1.Supplier_id = C.Supplier1)
left join User U1 on (U1.UserID = C.Supplier1)
left join Supplier S2 on (SS.Supplier_id = C.Supplier2)
left join User U2 on (U2.UserID = C.Supplier2)
where
((not C1.Supplier1 is null) and (U1.UserID is null))
or ((not C2.Supplier1 is null) and (U2.UserID is null))
This could easily be more useful to use as the union of a supplier1 missing with supplier2 missing.
Each Student and Teacher have their own UNIQUE UserID
Is it possible to retrieve the values from the three tables at once?
such that: it will display the UserID owned by each Student or Teacher?
I've tried the following query, but it doesn't work:
SELECT u.UserID, StudentID, TeacherID
FROM User u
INNER JOIN (SELECT * FROM Student, Teacher) ss
ON u.UserID = ss.UserID
Maybe this way:
SELECT u.UserID, S.StudentID, T.TeacherID
from User U
left join Student S on S.UserID = u.UserID
left join Teacher T on T.UserID = U.UserID
i have the following tables:
Table: People (id, first, last, age, phone, etc . .)
Table: Roles (id, name)
Table: Skills (id, name)
Table: People_Roles (id, personID^, roleID^)
Table: People_Skills (id, personID^, skillID^)
^ = foreign key
I basically want a query that gives me the full result set of all people and their roles and there skills.
Person.First, Person.Last, Roles.Name, Skills.Name
SELECT p.first, p.last, r.name, s.name
FROM People p
LEFT JOIN People_Roles pr
ON pr.personID = p.id
INNER JOIN Roles r
ON pr.roleID = r.id
LEFT JOIN People_Skills ps
ON ps.personID = p.id
INNER JOIN Skills s
ON ps.skillID = s.id
This query will select you all the people, even those without Roles or Skills assigned.
This query will work:
SELECT Person.First, Person.Last, Roles.Name, Skills.Name
FROM People
LEFT JOIN People_Skills
ON People.id = People_Skills.personID
INNER JOIN Skills
ON People_Skills.skillID = Skills.id
LEFT JOIN People_Roles
ON People.id = People_Roles.personID
INNER JOIN Roles
ON People_Roles.roleID = Skills.id
By the way, why your "bridge" tables have their own ID. I would just use a compound key made of PersonID and the corresponding foreign key (skillsID or rolesID) to ensure that every pair can only be made once.