Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 165 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I do an UPDATE statement with JOIN in SQL Server?

#11
This should work in SQL Server:

update ud
set assid = sale.assid
from sale
where sale.udid = id
Reply

#12
For SQLite use the RowID property to make the update:

update Table set column = 'NewValue'
where RowID =
(select t1.RowID from Table t1
join Table2 t2 on t1.JoinField = t2.JoinField
where t2.SelectValue = 'FooMyBarPlease');
Reply

#13
Try this one, I think this will works for you


update ud

set ud.assid = sale.assid

from ud

Inner join sale on ud.id = sale.udid

where sale.udid is not null

Reply

#14
The simplest way is to use [the Common Table Expression (CTE)][1] introduced in SQL 2005

with cte as
(select u.assid col1 ,s.assid col2 from ud u inner join sale s on u.id = s.udid)
update cte set col1=col2




[1]:

[To see links please register here]

Reply

#15
Teradata Aster offers another interesting way how to achieve the goal:

MERGE INTO ud --what table should be updated
USING sale -- from what table/relation update info should be taken
ON ud.id = sale.udid --join condition
WHEN MATCHED THEN
UPDATE SET ud.assid = sale.assid; -- how to update
Reply

#16
For prestashop users who use MySQL 5.7

```
UPDATE
ps_stock_available sa
INNER JOIN ps_shop s
ON sa.id_shop = s.id_shop AND s.id_shop = 1
INNER JOIN ps_order_detail od
ON sa.id_product = od.product_id AND od.id_order = 22417
SET
sa.physical_quantity = sa.quantity + sa.reserved_quantity
```


This was an example but the point is as Eric said here

[To see links please register here]


You need to add `UPDATE` statement at FIRST with the full address of all tables to join with, THEN add the `SET` statement
Reply

#17
Syntax strictly depends on which SQL DBMS you're using. Here are some ways to do it in ANSI/ISO (aka should work on any SQL DBMS), MySQL, SQL Server, and Oracle. Be advised that my suggested ANSI/ISO method will typically be much slower than the other two methods, but if you're using a SQL DBMS other than MySQL, SQL Server, or Oracle, then it may be the only way to go (e.g. if your SQL DBMS doesn't support `MERGE`):

ANSI/ISO:

update ud
set assid = (
select sale.assid
from sale
where sale.udid = ud.id
)
where exists (
select *
from sale
where sale.udid = ud.id
);

MySQL:

update ud u
inner join sale s on
u.id = s.udid
set u.assid = s.assid

SQL Server:

update u
set u.assid = s.assid
from ud u
inner join sale s on
u.id = s.udid

PostgreSQL:

update ud
set assid = s.assid
from sale s
where ud.id = s.udid;

Note that the target table must not be repeated in the `FROM` clause for Postgres. Main question:

[To see links please register here]



Oracle:

update
(select
u.assid as new_assid,
s.assid as old_assid
from ud u
inner join sale s on
u.id = s.udid) up
set up.new_assid = up.old_assid

SQLite:

update ud
set assid = (
select sale.assid
from sale
where sale.udid = ud.id
)
where RowID in (
select RowID
from ud
where sale.udid = ud.id
);

SQLite 3.33 added support for an `UPDATE` + `FROM` syntax analogous to the PostgreSQL one:

```
update ud
set assid = s.assid
from sale s
where ud.id = s.udid;
```

Main question:

[To see links please register here]

Reply

#18
To perform an UPDATE statement with a JOIN in SQL Server, you can use the JOIN syntax in combination with the UPDATE statement. Here's an example query that should update the ud table based on the corresponding values from the sale table:

UPDATE ud
SET ud.assid = sale.assid
FROM ud
JOIN sale ON ud.id = sale.udid;

In this query, the ud table is being updated, and the JOIN is performed between the ud and sale tables based on the matching id and udid columns, respectively. The SET clause specifies the column to be updated, ud.assid, and assigns it the value from the sale.assid column.

[dbForge Studio'se][1] SQL editor empowers you to execute your queries and proficiently manage your databases.


[1]:

[To see links please register here]

Reply

#19
# MySQL

You'll get the best performance if you forget the where clause and place all conditions in the ON expression.

I think this is because the query first has to join the tables and then runs the where clause on that, so if you can reduce what is required to join then that's the fasted way to get the results/do the update.

## Example

### Scenario

You have a table of users. They can log in using their `username` or `email` or `phone_number`. These accounts can be active (1) or inactive (0). This table has 50000 rows

You then have a table of users to disable (blacklist) at one go because you find out they've all done something bad.

A script runs that checks for users in the `blacklist_users` table and disables them in the `users` table.

This `blacklist_users` table however, only has one column with `usernames`, `emails` and `account numbers` all mixed together.

The `blacklist_users` table also has a "has_run" indicator which needs to be set to 1 (true) when it has been run so it can be skipped in future queries.

So if you have a WHERE clause here, internally, the results are getting brought back in the join and then the WHERE clause is being queried against that dataset. Instead, we can move all the where clause conditions into the join, and internally, remove the second query completely.

Therefore, this is the most optimal query to avoid needless lookups of the `users` table...

### Query

UPDATE users User
INNER JOIN
blacklist_users BlacklistUser
ON
(
User.username = BlacklistUser.account_ref
OR
User.email = BlacklistedUser.account_ref
OR
User.phone_number = BlacklistUser.account_ref
AND
User.is_active = 1
AND
BlacklistUser.has_run = 0
)
SET
User.is_active = 0,
BlacklistUser.has_run = 1;

### Reasoning

If we had to join on just the OR conditions it would essentially need to check each row 4 times (1 for `email`, 1 for `phone_number`, 1 for `username`, 1 for `is_active`) to see if it should join, and potentially return a lot more rows. However, by giving it more conditions it can "skip" every row we're not updating.

### Bonus

It's more readable. All the conditions are in one place and the rows to update are in another place.

I hope all that makes sense.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through