MYSQL Update using inner join cross using two tables issue - mysql

I'm trying to set a post to trash based on the title from another column in another table. Below is the code I have been trying to use, but so far the solution escapes me:
UPDATE egDuqUe_5_posts
SET egDuqUe_5_posts.post_status = 'trash'
FROM
egDuqUe_5_posts
INNER JOIN
egDuqUe_7_posts.id = egDuqUe_7_wc_order_coupon_lookup.coupon_id
WHERE
egDuqUe_5_posts.post_title = egDuqUe_7_posts.post_title

After a cup of tea, I came to this solution:
UPDATE egDuqUe_5_posts m
JOIN
egDuqUe_7_posts s
ON m.post_title = s.post_title
JOIN
egDuqUe_7_wc_order_coupon_lookup c
ON c.coupon_id = s.ID
SET m.post_status = 'trash'
WHERE m.post_type ='shop_coupon' && m.post_title = s.post_title

Related

How to update a table setting the where from a select from another table?

I have to update component_phase with the Cost from levelfour but also I have to join plancomponent to get the keys.
I think Im close to the answer, but it is not right.
component_phase
*idPlanComponent
costEstimate
plancomponent
*idPlanComponent
*LevelFourId
*idPlan
levelfour
*LevelFourId
Cost
UPDATE component_phase
SET costEstimate = (SELECT Cost FROM levelfour, plancomponent
WHERE levelfour.LevelFourId = plancomponent.LevelFourId
AND idPlan = :idPlan)
WHERE idPlanComponent in (SELECT idPlanComponent
FROM plancomponent
WHERE idPlan = :idPlan
You can use MySQL UPDATE JOIN to do the update.
UPDATE component_phase cp
INNER JOIN plancomponent p ON cp.idPlanComponent = p.idPlanComponent
INNER JOIN levelfour l ON l.LevelFourId = p.LevelFourId
SET cp.costEstimate = l.Cost
WHERE p.idPlan = :idPlan;

join twice on the same table rails?

I want to join on the same table twice to get username and username_to
#array = UsersWalletsBalancesFrozen.
joins("INNER JOIN userds userdsidto ON transactions.user_id_to=userdsidto.id").
joins("INNER JOIN userds userdsid ON transactions.user_id = userdsid.id").select("*")
This get me the last join only works i think it's overrides is there any solution to that
Just as a quick solution... Have you tried to put it in the same method call:
#array = UsersWalletsBalancesFrozen.joins("INNER JOIN userds AS user_to userdsidto ON transactions.user_id_to=user_to.id INNER JOIN userds userdsid AS user_from ON transactions.user_id = user_from.id").select("transactions.*, user_to.username, user_from.username")
?
PS: select('*') will be called by default. No need to explicitly specify it.

PHP MySQL Multiple Update

I have 2 tables that need updating based on 2 where clauses
i included a 3rd table which would join the other 2 tables together. i cant get either working.
UPDATE (list INNER JOIN Players ON list.Team_ID = Players.Players_Team_ID) INNER JOIN Users ON list.Team_ID = Users.User_Team_ID
SET
Players.Players_Team_ID = 6, Users.users_bank = users_bank-15000000, list.transfers = list.transfers+1
WHERE Users.User_ID=14 AND Players.Players_ID=3;
Without the 3rd table having an update it would be
UPDATE (list INNER JOIN Players ON list.Team_ID = Players.Players_Team_ID) INNER JOIN Users ON list.Team_ID = Users.User_Team_ID
SET
Players.Players_Team_ID = 6, Users.users_bank = users_bank-15000000
WHERE Users.User_ID=14 AND Players.Players_ID=3;
Can anyone help me get this working?
You can change your query to be like below using update-join syntax but I don't see why you need to JOIN with other tables. Your UPDATE statements could be single or individual update as well
UPDATE list,Players,users
INNER JOIN Players ON list.Team_ID = Players.Players_Team_ID
INNER JOIN Users ON list.Team_ID = Users.User_Team_ID
SET Players.Players_Team_ID = 6,
Users.users_bank = users_bank - 15000000,
list.transfers = list.transfers + 1
WHERE Users.User_ID=14
AND Players.Players_ID=3;

MariaDB update error inner join and select

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 :)

How to update table records with another table

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