Mysql Can I make an update with join - mysql

I have a database with several tables and all my queries work but I would like to had a new one and can't succeed to make it working.
Two tables 'vins' and 'producteurs'with a foreign key IDproducteur in my 'vins' table.
I need to update the 'vins'table from an excel source where the data are not always written corectly and need to change the stock value for some records of the 'vins' table.The p.nomproducteur and V.annee and v.nom are parameters but for test I put hard values.
My Query is the following:
UPDATE vins AS v
JOIN producteurs p
ON ( p.IDproducteur = v.IDproducteur AND p.nomproducteur LIKE "Charles" )
SET stock = 10
WHERE v.nom LIKE "Symphonie" AND v.annee = 2013
I have two records where the producteur is "Charles" and V.nom is Symphonie one withe annee = 2010 and one with 2013. I got no error in phpMyadmin but there is no results to my Query even by changing some command order.
Is what I want impossible to do?.

Put the condition p.nomproducteur LIKE "Charles" in the WHERE clause:
UPDATE vins v
JOIN producteurs p ON p.IDproducteur = v.IDproducteur
SET stock = 10
WHERE
v.nom = "Symphonie" AND v.annee = 2013 AND p.nomproducteur = "Charles"
Also there is no need for LIKE in this case, a simple = will do.

The update based on JOIN is commonly used in mysql so be sure that your join and where condition really match the values in your tables
UPDATE vins AS v
INNER JOIN producteurs p ON p.IDproducteur = v.IDproducteur
SET v.stock = 10
WHERE v.nom = "Symphonie"
AND v.annee = 2013
AND p.nomproducteur = "Charles"
and do the fact you are not filter for partial string use = instead of like

Related

How to update values in two tables where one is usermeta MySQL

I have two tables and I need to make single SQL request which will update values in both of them.
Their releation is based on ID (1 table = evdb_users.ID / 2 table = evdb_usermeta.user_id)
For now I did some SQL query like this:
"UPDATE evdb_users
INNER JOIN evdb_usermeta ON evdb_users.ID = evdb_usermeta.user_id AND evdb_usermeta.meta_key='phone_number'
SET evdb_users.user_login='%s', evdb_users.user_email='%s', evdb_users.display_name='%s', evdb_usermeta.meta_value='%s'
WHERE evdb_users.ID=%d"
Data in evdb_users (login/email/display name) - are updating, but nothing changes in usermeta (phone_number)
Please help me to figure out how it should be and how it must works.
try this:
UPDATE evdb_users
INNER JOIN evdb_usermeta
ON evdb_users.ID = evdb_usermeta.user_id
AND evdb_usermeta.meta_key='phone_number'
SET evdb_users.user_login='%s'
, evdb_users.user_email='%s'
, evdb_users.display_name='%s'
, evdb_usermeta.meta_value='%s'
WHERE evdb_users.ID=%d;

How do I add a criteria to a query on one table but keep all the records from another?

I have the following query:
SELECT games_atp.ID1_G, odds_atp.K1
FROM games_atp LEFT JOIN odds_atp ON (games_atp.ID1_G = odds_atp.ID1_O) AND (games_atp.ID2_G = odds_atp.ID2_O) AND (games_atp.ID_T_G = odds_atp.ID_T_O) AND (games_atp.ID_R_G = odds_atp.ID_R_O)
I know the joining is convoluted but the original db is built without a primary key. The above works fine and importantly pulls all the records from games_atp. I now want to add a criteria into this to pull only certain K1 records from odds_atp. I added a WHERE clause as follows:
SELECT games_atp.ID1_G, odds_atp.K1
FROM games_atp LEFT JOIN odds_atp ON (games_atp.ID1_G = odds_atp.ID1_O) AND (games_atp.ID2_G = odds_atp.ID2_O) AND (games_atp.ID_T_G = odds_atp.ID_T_O) AND (games_atp.ID_R_G = odds_atp.ID_R_O)
WHERE (((odds_atp.ID_B_O)=2));
However, this overides the left join and only pulls records from games_atp where there is a corresponding record in odds_atp with ID_B_O = 2. How do I keep the criteria and all the records in games_atp? Thanks in advance.
Your current where condition will filter your final result, hence you are only seeing id_B_O = 2.
However, you could also add the wehre condition directly into your left join.
something like this.
SELECT
games_atp.ID1_G, odds_atp.K1
FROM
games_atp
LEFT JOIN odds_atp ON
(
(odds_atp.ID_B_O =2)
AND
(
(games_atp.ID1_G = odds_atp.ID1_O)
AND (games_atp.ID2_G = odds_atp.ID2_O)
AND (games_atp.ID_T_G = odds_atp.ID_T_O)
AND (games_atp.ID_R_G = odds_atp.ID_R_O)
)
);
or you could also take advantage of sub-queries

Microsoft Access Update Statement with Inner Joins and Subqueries

I am having a tough time figuring out how to do this update query. Basically I need to update a table named tblOpenJobs. It needs to be updated with the dbo_WorkOrders table with the Max Install date. But there is not direct relationship between those two tables you need to have the dbo_premise table between. Here is my query, what am I doing wrong?
UPDATE tblOpenJobs
INNER JOIN (dbo_Premise INNER JOIN dbo_WorkOrders w (WHERE w.InstallDate IN
(SELECT MAX(InstallDate) FROM dbo_WorkOrders WHERE dbo_WorkOrders.PremiseKey = w.PremiseKey))
ON (dbo_Premise.PremiseKey = w.PremiseKey)
ON tblOpenJobs.ServiceOrderNum = dbo_Premise.AccountNumber
SET tblOpenJobs.InstallerID = w.InstallerID,
tblOpenJobs.InstallDate= w.InstallDate,
tblOpenJobs.New_Serial_num= w.NewSerial,
tblOpenJobs.Old_Reading= w.OldRead;
I checked this in Access 2007 query window:
Your query seems neither Transact-SQL, neither Access, as the two have different syntax.
In Access, table aliasing must use the keyword AS, while Transact-SQL does not require:
UPDATE ((tblOpenJobs
INNER JOIN dbo_Premise
ON tblOpenJobs.ServiceOrderNum = dbo_Premise.AccountNumber)
INNER JOIN dbo_WorkOrders AS w
ON dbo_Premise.PremiseKey = w.PremiseKey)
SET tblOpenJobs.InstallerID = w.InstallerID,
tblOpenJobs.InstallDate = w.InstallDate,
tblOpenJobs.New_Serial_num = w.NewSerial,
tblOpenJobs.Old_Reading = w.OldRead
WHERE (w.InstallDate IN
(SELECT MAX(InstallDate)
FROM dbo_WorkOrders
WHERE dbo_WorkOrders.PremiseKey = w.PremiseKey))
This is correct in syntax, but I'm not sure it can update your data, as multi-table linked update is not easy in Access.

Update table values from another table using joins?

I am trying to update a collection of computer hostnames to match recently changed room numbers. The hostnames in the database are formatted like FL-itf2106a with 2106 being the old room number. I already have a list in another table that has the old and new room numbers on the same row. I have been trying to strip all the non-numerics out of the string for the hostname and join that to the updates table unsuccessfully.
UPDATE computers c
INNER JOIN updates u
ON u.old = (
SELECT NumericOnly(c.hostname)
WHERE hostname
LIKE "%FC%"
)
SET c.hostname = CONCAT('classf', u.new);
NumericOnly is a User function that removes all characters but numerics from a string.
I am trying to set the hostname column equal to classf + the new room number.
I'd take my chances with something like
UPDATE computers c
INNER JOIN updates u
ON u.old = NumericOnly(c.hostname)
SET c.hostname = CONCAT('classf', u.new)
WHERE c.hostname LIKE "%FC%";
Postgresql version that worked for me:
update table1 set columnZ = newValue from table1 t1
inner join table2 t2
on t1.columnX = t2.columnY
where table1.uniqueID = t1.uniqueID
and t2.filterColumn = filterValue
http://johnstewien.wordpress.com/2010/03/15/doing-a-join-in-an-update-in-sql-for-postgresql/
This question comes up in the first few hits for "SQL UPDATE JOIN" on google.

Update using Select Statement

I wanna write a query like this :
UPDATE `test_credit`
SET `test_credit`.`credit`=(`test_credit`.`credit`-((`test_credit`.`credit`/100)*5))
WHERE `test_credit`.`name` = `users`.`uname`
in fact i want to get a query on users.uname = test_credit.name but mysql say it has error and realize users.uname as column
what is correct query ?
You need to explicitly join it with table users. As far from my understanding based on your query, you want to calculate the credit if the names exists on both tables.
Give this a try,
UPDATE test_credit a
INNER JOIN users b
ON a.name = b.uname
SET a.credit = (a.credit - ((a.credit/100) * 5.0))
-- WHERE b.parent= "example"