UPDATE with INNER JOIN on SQL Server - sql

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

Related

Update SQL Query with where condition

I added a column in the Orders table called EventId and I have to fill the column with the right value for every row.
I want to achieve it with a more complex sql query like below:
UPDATE [dbo].[Orders] o
SET o.EventId = ????
WHERE o.Id IN (SELECT o.id, s.eventid FROM orders o
INNER JOIN orderskudiscounts osd ON o.id = osd.orderid
INNER JOIN skus s ON osd.skuid = s.id GROUP BY o.id, s.eventid)
I not sure how can I write the query successfully... I have more than 2 thousand mapping to do. So I should use a query...
Thank for any help
I am guessing that you want an update like this:
UPDATE o
SET o.EventId = s.eventid
FROM orders o INNER JOIN
orderskudiscounts osd
ON o.id = osd.orderid INNER JOIN
skus s
ON osd.skuid = s.id;

SQL SELECT statement

I need to make an sql SELECT statement where I will join tables(or get the value from joined table) only if condition is met, is this possible ?
I have an order table where I have user IDs, but I also have random generated IDs for users who ordered as guests. And I want to join users table on orders table, with that ID, but when the ID is randomly generated I want to return only values from order table because there are not records in user table for that ID.
I have only this, which will write rows where user_id exists in both tables
$sql = "SELECT orders.id_o, orders.user_id, orders.price, users.username
FROM orders JOIN users
ON orders.user_id = users.id
ORDER BY order_date ASC";
This is exactly what left joins are for. To answer the followup question in the comments, you can use coalesce to replace the nulls returned from the left join:
SELECT orders.id_o, orders.user_id, orders.price,
COALESCE(users.username, 'Guest')
FROM orders
LEFT JOIN users ON orders.user_id = users.id
ORDER BY order_date ASC
Nearly, just change to left join
$sql = "SELECT orders.id_o, orders.user_id,orders.price, users.username
FROM orders Left JOIN users
ON orders.user_id = users.id
ORDER BY order_date ASC";
The following statement should do it:
SELECT
orders.id_o,
orders.user_id,
orders.price,
users.username
FROM orders
LEFT JOIN users
ON orders.user_id = users.id
ORDER BY order_date ASC

TSQL: How to update this field

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

SQL INNER JOIN exception

I'm new to SQL queries and I'm trying to join two tables
I need to get all data of the followers of userID = 2
here's the error i get : Syntax error: Encountered "INNER" at line 1, column 39.
and here's the SQL query I ran :
SELECT * FROM FOLLOWER
WHERE userID = "2"
INNER JOIN USERS ON FOLLOWER.Follower_userID = USERS.userID
ORDER BY USERS.follower_count ASC
The tables in my DB are :
FOLLOWER
ID
userID
Follower_userID
USERS
userID
username
password
Nickname
P.S
I'm using Apache Derby.
Thank you so much guys.
position of where clause was incorrect
structure of SELECT query is
SELECT fields
FROM tables
WHERE conditions
ORDER BY fields
so you query should be
SELECT *
FROM FOLLOWER INNER JOIN USERS ON FOLLOWER.Follower_userID = USERS.userID
WHERE userID="2"
ORDER BY USERS.follower_count ASC
Try this statement:
SELECT * FROM FOLLOWER Fl
WHERE userID="2"
INNER JOIN USERS Us ON Us.userID = Fl.Follower_userID
ORDER BY USERS.follower_count ASC
Let me know if it's works
First use join and then where.
SELECT * FROM FOLLOWER INNER JOIN USERS ON FOLLOWER.Follower_userID = USERS.userID WHERE userID=2 ORDER BY USERS.follower_count ASC
Try this
SELECT * FROM FOLLOWER
INNER JOIN USERS
ON FOLLOWER.Follower_userID = USERS.userID
WHERE FOLLOWER.userID="2"
ORDER BY USERS.follower_count ASC
Hope this helps
The Inner Join syntax
SELECT *
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.Table1ColName = T2.Table2ColName
ORDER BY T1.Table1ColName
Refer this link for Inner Join
Change your query like
SELECT * FROM FOLLOWER
INNER JOIN USERS
ON FOLLOWER.Follower_userID = USERS.userID
WHERE userID="2"
ORDER BY USERS.follower_count ASC
The rules of where with join is first made all join and then give where condition which filter more data..
so your where condition just put after inner join as above all suggested.
select *
from yourtable
join yourothertable
where condition if you want
http://bytes.com/topic/sql-server/answers/850159-performance-conditions-where-clause-vs-conditions-inner-join

sql, sqlite SELECT with inner join

I'm wondering how to select one column twice using an inner joinor some other way. my database is sqlite and i use PDO db driver.
My Example:
SELECT
orders.id,
orders.order_number,
clients.first_name,
clients.last_name,
users.name AS user_name
FROM orders
INNER JOIN clients ON
orders.client_id = clients.id
INNER JOIN users ON
orders.created_by = users.id
I want to get also, the user_name who edited this record
orders.edited_by = users.id
How to join this selection?
You'll need to use table aliases.
SELECT
orders.id,
orders.order_number,
clients.first_name,
clients.last_name,
creator.name AS creator_user_name
editor.name AS editor_user_name
FROM orders
INNER JOIN clients ON
orders.client_id = clients.id
INNER JOIN users creator ON
orders.created_by = creator.id
INNER JOIN users editor ON
orders.edited_by = editor.id
Use aliases in your table names, so you can use multiple references to the same table. This also can help make large queries easier to read.
SELECT
orders.id,
orders.order_number,
clients.first_name,
clients.last_name,
createUsers.name AS creator_name,
editUsers.name AS editor_name
FROM orders
INNER JOIN clients ON
orders.client_id = clients.id
INNER JOIN users As createUsers ON
orders.created_by = users.id
INNER JOIN users As editUsers ON
orders.edited_by = users.id
You can use as many "instances" of the same table as you wish.