I have a problem with updating records in my table. I am doing research all day but it is just beyond me.
Basics: I have two tables
TABLE1
TABLE2
I need to update nr_g from TABLE1 with id from TABLE2 but only where 'kraj' 'region' 'nazwa_hotelu' from TABLE1 is equal 'country' 'region' 'hotelName' from TABLE2
my trying so far:
UPDATE merlinx u
LEFT JOIN
merlinx_new s ON u.nr_g != s.id
SET
u.nr_g = s.id
WHERE
u.kraj = s.country AND u.nazwa_hotelu = s.hotelName AND u.region = s.region
That is updating me only 4 rows... and 1592 are unsafe statements
another shot of mine:
UPDATE merlinx_merged
SET
nr_g = (SELECT
merlinx_new.id
FROM
merlinx_new
INNER JOIN
merlinx_merged
WHERE
merlinx_new.country = merlinx_merged.kraj
AND merlinx_new.hotelName = merlinx_merged.nazwa_hotelu
AND merlinx_new.region = merlinx_merged.region)
And that is just throwing errors.
My mind is fried after 8 hours wasted on it. Help is much appreciated.
I think your issue is in your join statement. You have
LEFT JOIN merlinx_new s ON u.nr_g != s.id
You shouldn't have to have the ON criteria in that (if I'm understanding your question correctly).
This should do the trick if you want to overwrite merlinx.nr_g with the value from merlinx_new.id if all of your criteria matches in the WHERE clause.
UPDATE merlinx u, merlinx_new s
SET u.nr_g = s.id
WHERE u.kraj = s.country AND u.nazwa_hotelu = s.hotelName AND u.region = s.region
Related
Im trying to change some records in my database but sadly my sql knowledge is a bit limited. After googling and reading stuff online I have managed to write a select statement in which i can find the records that I want to update but i dont understand the logic to write the update statement to do it. I have to make several similar update statements so I hope this one I can figure out the rest myself
This is the select statement I have:
SELECT
MG.id,
MG.status,
MG.fin,
MG.execDateTime,
EXISTS
(
SELECT 1
FROM Mtask T
JOIN MTaskHis TH ON TH.t_id= T.id
WHERE T.tg_id = MG.id
AND YEAR(TH.dateTime) = 2019
) AS hasExecStart,
NMG.id,
NMG.execDateTime,
EXISTS
(
SELECT 1
FROM Mtask T
JOIN MTaskHis TH ON TH.t_id = T.id
WHERE T.tg_id = NMG.id
AND YEAR(TH.dateTime) = 2019
) AS hasExecNext
FROM Management_Group MG
JOIN MT_Groupman MTGM ON
MG.tgm_id = MTGM.id
LEFT JOIN Management_Group NMG ON MTGM.id =
NMG.tgm_id AND YEAR(NMG.execDateTime) = 2019
JOIN Management_Man MM ON MTGM.man_id = MM.id
JOIN Location L ON MM.location_id = L.id
WHERE L.org_id = 69
AND MG.stat != 'DELETED'
AND YEAR(MG.execDateTime) = 2018
AND MM.Type= 9
AND MG.fin != 1
AND EXISTS
(
SELECT 1
FROM Mtask T
WHERE T.tg_id = MG.id
AND T.stat = 'execution'
)
HAVING hasExecNext = 0 AND hasExecStart = 1
I know standard updates in sql:
UPDATE <TABLENAME>
SET <fieldName> = <value>
WHERE <conditons>
Except I do not know how to convert this select statement I have made into an update statement, reason for that is:
- Where do I put the exist alias in the update statement
- I also dont understand when or where to put all the JOINS in the from statement
- What about the HAVING
What is the best way to do joined updates like this?
In an UPDATE you can join the table you want to update to a sub-query that contains your current query.
UPDATE YourTable t
JOIN
(
<< add your query here >>
) q ON q.SomeKeyField = t.SomeKeyField
SET t.FieldName = q.FieldNameFromSubquery,
t.OtherFieldName = q.OtherFieldNameFromSubquery
Hi and sorry if I don't do the question the correct way
I have this query :
SELECT
t4.tar_nombre as nombre_tarea,
t2.rdi_fechacti as fecha_actividad,
t3.rea_hrstarea as horas_trabajadas
FROM
act_usuario t1
INNER JOIN
act_regisdiario t2
ON
(t2.usu_id = t1.usu_id)
INNER JOIN
act_registtarea t3
ON
(t3.rdi_id = t2.rdi_id)
INNER JOIN
act_tarea t4
ON
(t4.tar_id = t3.tar_id)
WHERE
t4.usu_id = 4
GROUP BY
t3.rea_id
ORDER BY
t2.rdi_fechacti
And the query print this :
So, I need when in "fecha_Actividad" exist the same "nombre_actividad" , thats only show me one "fecha_actividad" and the sum of "horas_trabajadas".
For example the query show this:
But I need this (because the same "nombre_tarea" is in the same date):
The reference:
Sorry my english.
You can simply use SUM(field) syntax after grouping.
SELECT
t4.tar_nombre as nombre_tarea,
t2.rdi_fechacti as fecha_actividad,
t3.rea_hrstarea as horas_trabajadas,
SUM(t3.rea_hrstarea)
FROM
act_usuario t1
INNER JOIN
act_regisdiario t2
ON
(t2.usu_id = t1.usu_id)
INNER JOIN
act_registtarea t3
ON
(t3.rdi_id = t2.rdi_id)
INNER JOIN
act_tarea t4
ON
(t4.tar_id = t3.tar_id)
WHERE
t4.usu_id = 4
GROUP BY
t4.tar_nombre
ORDER BY
t2.rdi_fechacti
There are also other functions you can use on grouped results. You can find them here.
I am trying to write the following update statement;
UPDATE #eticat
SET eticat_purchase_total = t.eticat_purchase_total
FROM (
SELECT eticat_id, COUNT(eticat_id) as eticat_purchase_count
FROM etransaction
INNER JOIN etransaction_item
INNER JOIN etransaction_item_catalog ON eti_eticat_id = eticat_id
ON eti_et_id = et_id
WHERE et_cmc_id = #can_cmc_id
GROUP by eticat_id
) as t
WHERE eticat_id = t.eticat_id
But it keeps complaining about ambigous columns. Can someone please tell me what I am doing wrong.
EDIT: Error Message is "Ambiguous column name 'eticat_id'."
That line is 'WHERE eticat_id = t.eticat_id'
First, that's not a CTE; it's a derived table. Similar, but different :)
Second, you're updating a table variable that's not included in your FROM clause, which is confusing SQL Server. Try something like:
UPDATE x
SET eticat_purchase_total = t.eticat_purchase_total
FROM (
SELECT eticat_id, COUNT(eticat_id) as eticat_purchase_count
FROM etransaction
INNER JOIN etransaction_item
INNER JOIN etransaction_item_catalog ON eti_eticat_id = eticat_id
ON eti_et_id = et_id
WHERE et_cmc_id = #can_cmc_id
GROUP by eticat_id
) as t JOIN #eticat x ON x.eticat_id = t.eticat_id
I have been trying to update the data behind a SELECT query, but not getting too far, so I have created a new database, with the hope of finding a solution.
I have two tables:
table_a and table_b
table_a:
ID, item_to_match, new_value *(this is initially blank)*
table_b:
IDB, item_to_match, new_value *(the value I want to get into table_a)*
Somehow, I want to update new_value in table_a with new_value from table_b - but I can't seem to get it (I am new to this).
I tried creating an UPDATE query, but that only allowed me to show one of the two tables. So I tried creating a master query (called main), thinking I could create an UPDATE query on that - but I can't get that to work either. Here is the syntax:
SELECT
`table_a`.`new_value` AS `goingto`,
`table_b`.`new_value` AS `takenfrom`
FROM (`table_a`
JOIN `table_b`
ON ((`table_a`.`item_to_match` = `table_b`.`item_to_match`)))
I really, really would appreciate a bit more guidance on how to do this (sorry to keep asking).
I have now tried three variations and keep getting the same error message. But here are the variations:
UPDATE table_a, table_b
SET table_a.new_value = table_b.new_value
WHERE table_a.item_to_match = table_b.item_to_match
UPDATE table_a a
INNER JOIN table_b b ON a.item_to_match = b.item_to_match
SET a.new_value = b.new_value
UPDATE table_a a,table_b b
SET a.new_value = b.new_value
WHERE a.item_to_match = b.item_to_match
The field item_to_match is not a primary key or indexed (FYI)...
What the heck am I doing wrong???
I also just tried:
UPDATE table_a a
JOIN table_b b
ON a.item_to_match = b.item_to_match
SET a.new_value = b.new_value
Is the syntax right....am I doing something wrong in the app itself?
Here is how you can do it
UPDATE table_a AS a
INNER JOIN table_b AS b ON a.item_to_match = b.item_to_match
SET a.new_value = b.new_value
You don't need to say which table(s) you're updating, that's implicit in your SET clause.
UPDATE tableA a
JOIN tableB b
ON a.a_id = b.a_id
JOIN tableC c
ON b.b_id = c.b_id
SET b.val = a.val+c.val
WHERE a.val > 10
AND c.val > 10;
There is no FROM clause.
Please refer this question asked in SO.
I hope you can help me again, thanks already for pointing me to the right direction with creating the check digit for the new IBAN in Germany. I am now trying to update our membership database with the newly calculated BIC and IBAN but seem to have a problem with the UPDATE statement on the MariaDB MySQL database, despite the fact that I think I got the syntax right.
All I am trying to do is set the two fields "konto_bic" and "konto_iban" in the table "mitglieder" from the SELECT statement which creates a temporary table called b with the columns "id", "bic" and "iban". The "id" is the same in the two tables.
Here is my first try:
update a
set a.`konto_bic` = b.`BIC`, a.`konto_iban` = b.`IBAN`
from `mitglieder` a
INNER JOIN (SELECT m.`id`, m.`nachname`, m.`vorname`, m.`konto_bank`, m.`konto_blz`, m.`konto_nummer`, k.`bic` AS 'BIC', CONCAT('DE',LPAD(98-MOD(CONVERT(CONCAT(m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0'),'1314','00'), decimal(24)),97),2,'0'),m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0')) AS 'IBAN'
FROM `mitglieder` m
LEFT JOIN `konvert_bic_blz` k
ON m.`konto_blz` = k.`blz`
ORDER BY m.`nachname`, m.`vorname`) b
ON a.`id` = b.`id`
However, this produced an error and I tried this instead:
update `mitglieder` a
set a.`konto_bic` = b.`bic`, a.`konto_iban` = b.`iban`
FROM (SELECT m.`id` as 'id', k.`bic` as 'bic', CONCAT('DE',LPAD(98-MOD(CONVERT(CONCAT(m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0'),'1314','00'), decimal(24)),97),2,'0'),m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0')) AS 'iban'
FROM `mitglieder` m
LEFT JOIN `konvert_bic_blz` k
ON m.`konto_blz` = k.`blz`) b
WHERE a.`id` = b.`id`
That also did not get me any further (error from DB).
Can anyone see what my syntax error might be?
Thank you in advance for your help
Stephan
Try below SQL
UPDATE `mitglieder` a,
(SELECT m.`id` AS 'id',
k.`bic` AS 'bic',
CONCAT('DE',LPAD(98-MOD(CONVERT(CONCAT(m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0'),'1314','00'), decimal(24)),97),2,'0'),m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0')) AS 'iban'
FROM `mitglieder` m
LEFT JOIN `konvert_bic_blz` k ON m.`konto_blz` = k.`blz`) b
SET a.`konto_bic` = b.`bic`, a.`konto_iban` = b.`iban`
WHERE a.`id` = b.`id`
UPDATE Syntax
http://dev.mysql.com/doc/refman/5.0/en/update.html
I tried this on MariaDB 10.2.6 :
SET sql_mode = 'ANSI_QUOTES'
UPDATE "test"."user" AS "a"
INNER JOIN "test"."user_profile" AS "c" ON "c".user_id = "a".id
INNER JOIN "test"."profile" AS "d" ON "d".id = "c".profile_id
SET "a".firstname = 'laurent'
WHERE "a".id = 3;
And it works :)