SQL syntax error. MySQL Update table SELECT - mysql

I want to update a row based on the existence of an id in other table that is also in that table. In other words that id is a primary key in other table and just a column in the table I want to update.
I want to update based in the same id.
I have this query but it doesn't work because of the SQL syntax.
UPDATE
transaction
SET
DaysRented = 3,
Cost = 3,
TotalCost= 5
FROM
transaction
INNER JOIN
rentals
ON
transaction.idRentals = rentals.idRentals;

syntax for mysql
http://www.mysqltutorial.org/mysql-update-join/
First, you specify the main table ( T1) and the table that you want
the main table to join to ( T2) after the UPDATE clause. Notice that
you must specify at least one table after the UPDATE clause. The
data in the table that is not specified after the UPDATE clause is
not updated.
Second, you specify a kind of join you want to use i.e., either
INNER JOIN or LEFT JOIN and a join condition. Notice that the JOIN
clause must appear right after the UPDATE clause.
Third, you assign new values to the columns in T1 and/or T2 tables
that you want to update.
UPDATE transaction
INNER JOIN rentals ON transaction.idRentals = rentals.idRentals
SET DaysRented = 3,
Cost = 3,
TotalCost = 5;

You are using SQL Server update/join syntax. The proper MySQL syntax is:
UPDATE transaction INNER JOIN
rentals
ON transaction.idRentals = rentals.idRentals
SET DaysRented = 3,
Cost = 3,
TotalCost = 5;

Related

Execute Join in If statement

I want to join a bunch of columns from different tables in the same database in mysql. The problem I have, is that not all values are written in the same table.
The concrete example: In one table there is the sid column. Depending on the value in this column the next values I need in this join are located in different tables. If the sid value equals 1 I have to join with table 2 to get the values I want, if the sid value is anything but 1 I have to join with table 3.
I researched a bit into if - else in mysql but all the information I can find is about changing values and how they are displayed under certain conditions.
I want the workflow to change under certain conditions.
No matter which join gets executed, the resulting table has the same amount of columns. I do not actually display any data from table 2 and table 3. The descr column exists in table 4. All the other displayed columns exist in table 1. So there should not be any problems wit the display.
The code looks as follows (inspired by python):
SELECT type, s_type, id, descr
FROM table1
IF sid = 1 THEN
LEFT JOIN table2
ON table1.sid = table2.sid
LEFT JOIN table4
ON table2.sid = table4.id_name
ELSE
LEFT JOIN table3
ON table1.sid = table3.svid
LEFT JOIN table4
ON table3.svid = table4.id_name
END IF;
What is the correct way to get this control flow done in mysql?
IF cannot be used by this in SQL. If I understand the logic, you could do something like this:
SELECT type, s_type, id, table4.descr
FROM table1 LEFT JOIN
table2
ON table1.sid = table2.sid AND table1.sid = 1 LEFT JOIN
table3
ON table1.sid = table3.svid AND table1.side <> 1 LEFT JOIN
table4
ON table4.id_name = COALESCE(table3.svid, table2.sid)
If your tables are large, the COALESCE() might be a performance killer.

SQL Update list of records coming from select

I need to update a list of records coming from a SELECT statement I'm trying with something of this kind but I got errors regarding the statement format:
UPDATE
noleggio_veicoli
SET
data_esportazione = CURDATE()
WHERE id IN
(SELECT id FROM noleggio_veicoli WHERE id_convenzionato = 3);
But it just returns:
Error Code: 1093. You can't specify target table 'noleggio_veicoli'
for update in FROM clause
It looks like MySQL is only able to run classic UPDATE i.e.:
UPDATE
noleggio_veicoli
SET
data_esportazione = CURDATE()
WHERE
id_convenzionato = 3;
Unlucky this's not what I need since SELECT query I need to chain to the UPDATE statement is more complex and depends on Views already defined in the SQL server.
Like #Maxim said, you should do something like
CREATE TEMPORARY TABLE IF NOT EXISTS temp AS (
SELECT id FROM noleggio_veicoli where id_convenzionato=3
);
update noleggio_veicoli set data_esportazione=curdate()
where id in (select id from temp);
If you want to update data that match a select p.id from parent p inner join child c on c.pid=p.id left join child2 c2 on c.x=c2.y where c.xx=yy and p.x=y... you can do something like:
update parent p
set aaa=bbbb
from child c
left join child2 c2 on c.x=c2.y
where c.pid=p.id and c.xx=yy and p.x=y ...
You can specify this with a join:
UPDATE noleggio_veicoli nv JOIN
noleggio_veicoli nv3
ON nv.id = nv3.id and nvid3.id_convenzionato = 3
SET data_esportazione = curdate();
For some odd reason MySql doesn't allow to reference the same table you update in a first level subquery.
But you can work around that via a subqueryinception. (It's not a real word, probably)
I.e. using a sub-query in the sub-query.
Example using an IN:
UPDATE noleggio_veicoli
SET data_esportazione = current_date
WHERE id IN (select id from (select id from noleggio_veicoli where id_convenzionato = 3) q);
It works also with an EXISTS:
UPDATE noleggio_veicoli as t
SET data_esportazione = current_date
WHERE EXISTS
(
select 1
from (select id, id_convenzionato from noleggio_veicoli) as t2
where t2.id_convenzionato = 2
and t2.id = t.id
);
You can test it here on rextester
You can't update and read from one table at the same time. At first you must
select necessary ids and save them to somewhere, for example to temporary table

Update field set to value of row in same field

I'm trying to update a specific field and set it equal to value of a row in the same field.
What have I tried so far is this:
mysql> UPDATE tblitem SET imagefilename = (SELECT imagefilename from tblitem where itemid=2) where itemid=1'
1093 - You can't specify target table 'tblitem' for update in from clause
What I am trying to do here is to update the value of itemid 1 to the value of itemid 2.
Is that even possible? Thank you in advance.
In MySQL, if you're doing an UPDATE/INSERT/DELETE queries on a table, you can't reference the said table in the inner query. One workaround is to use a subquery inside the inner query:
UPDATE tblitem
SET imagefilename =
(
SELECT imagefilename
FROM (SELECT * FROM tblitem) AS t
WHERE itemid = 2
)
WHERE itemid = 1
Use a join instead:
UPDATE tblitem t JOIN
(SELECT imagefilename from tblitem where itemid = 2
) t2
SET t.imagefilename = t2.imagefilename
WHERE itemid = 1;
The SQL standard and other databases allow you to refer to the table being updated elsewhere in the update statement. However, MySQL does not allow this. The JOIN is a simple enough work-around.

Delete, Update with derived tables?

I have just studied FROM clause and derived tables in mysql and most of the websites provided the examples using SELECT command
Example SELECT * FROM (SELECT * FROM usrs) as u WHERE u.name = 'john'
But when I have tried using delete or update command it does not seem to work.
Example DELETE FROM (SELECT * FROM usrs) as u WHERE u.name = 'john'
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near (SELECT * FROM usrs) as u WHERE u.name = 'john' at line
UPDATE (SELECT * FROM usrs) as u SET u.lname ='smith' WHERE u.name = 'john'
1288 The target table e of the UPDATE is not updatable
So derived tables does not work with delete or update commands? or is there a way to make it work.
Instead of writing the table name for update and delete I want to write a subquery that gets the records and perform the delete operation on that records? Is that possible in mysql?
UPDATED I have to delete a record and i have three tables, the record may exist in any of the table
My approach delete from first table rows effected? quit: else check second table rows effected? quit : else check third table
But if I use UNION ALL I can do this way
Delete from (select * from tb1 union all select * from tb2 union all select * from tb3) e as e.uname = 'john'
but this query does not seem to work , now could anyone tell me how do i delete or update a record when i have more than one table to search. Any help is greatly appreciated.
You can't directly delete from the subquery, but you can still use it if you'd like, you'll just need to use it in a JOIN:
DELETE usrs
FROM usrs
INNER JOIN (
SELECT * FROM usrs WHERE name = 'john'
) t ON usrs.Id = t.Id
Or you could use IN:
DELETE usrs
WHERE ID IN (
SELECT ID
FROM usrs
WHERE name = 'John'
)
With this said, for this example, I don't know why you'd want a subquery:
DELETE usrs WHERE name = 'John'
Edit base on comments. To delete from multiple tables at the same time, you can either have multiple DELETE statements, or you can use something like the following:
delete t1, t2, t3
from (select 'john' as usr) t
left join t1 on t.usr=t1.usr
left join t2 on t.usr=t2.usr
left join t3 on t.usr=t3.usr
SQL Fiddle Demo
Derived tables exist only for the duration of the parent query they're a member of. Assuming that this syntax and the operations were allowed by MySQL, consider what happens:
a) Your main query starts executing
b) the sub-query executes and returns its results as a temporary table
c) the parent update changes that temporary table
d) the parent query finishes
e) temporary tables are cleaned up and deleted
Essentially you'll have done nothing except waste a bunch of cpu cycles and disk bandwidth.
UPDATE queries DO allow you to join against other tables to use in the WHERE clause, e.g..
UPDATE maintable
LEFT JOIN othertable ON maintable.pk = othertable.fk
SET maintable.somefield='foo'
WHERE othertable.otherfield = 'bar'

update query in sql based on condition fro two tables

I need to build a update query. My existing code looks like this.
update table1
set data_plan=(select d.data_plan from table1 m,table2 d
where m.msidn = d.msidn and m.data_plan!=d.data_plan);
table 1 has columns msisdn and data_plan, table 2 also has same columns. I want to update the table1 data_plan column depending on some condition which I get through select query. But when I run the code I get this error.
You can't specify target table 'msisdn1' for update in FROM clause
Try it this way
update table1 m join table2 d
on m.msidn = d.msidn
and m.data_plan != d.data_plan
set m.data_plan = d.data_plan
Try this ...
UPDATE table1 SET m.data_plan=d.data_plan
FROM table1 m
INNER JOIN table2 d ON m.msidn = d.msidn and m.data_plan!=d.data_plan