MySQL Update Value from SubQuery - mysql

I'm using MySQL and am running a query, which I think should work, but apparently I'm missing something.
0 records get updated when I run:
UPDATE `client`
SET StatementTermsID = (SELECT StatementTermsID FROM statementterms WHERE TermsDescription = 'NET 15')
WHERE `client`.StatementNote LIKE '%Net 15%';
If I run the subquery by itself, I get the record id as expected. If I change the subquery to be a static value, then 2000 plus records get updated. Any idea on what I'm missing?

Here's a different syntax:
UPDATE `client` a,
(SELECT StatementTermsID FROM statementterms WHERE TermsDescription = 'NET 15') b
SET a.StatementTermsID = b.StatementTermsID
WHERE a.StatementNote LIKE '%Net 15%';

i Think you should use a join. I can see that we cannot use StatementTermsID because thats what you are trying to update.So for sure it won`t be there in the former table.
If you have any ids to join Use that as well.
UPDATE client
SET StatementTermsID =statementterms.StatementTermsID
FROM client
INNER JOIN statementterms
WHERE statementterms.TermsDescription = 'NET 15' AND
client.StatementNote LIKE '%Net 15%';
Or else Try this syntax
UPDATE client , statementterms
SET client.StatementTermsID = statementterms .StatementTermsID
WHERE statementterms.TermsDescription = 'NET 15' AND
client.StatementNote LIKE '%Net 15%';

Related

MySQL UPDATE with JOINS using MAX

I'm no MySQL guru.
I'm trying to update a table clients.converted from projects.last_update column.
DATETIME: clients.converted (new column as of now).
DATETIME: projects.last_update.
BOOLEAN: projects.converted.
For each client's project, there is the possibility to end the project with a prospect-to-client conversion, if so, (boolean) projects.converted will me TRUE.
What I want is to do an UPDATE statement on clients to fill clients.converted from MAX(projects.last_update) WHERE project's projects.converted = true.
So far I have tried a couple of queries, but this one grasps the idea in a less-confusing way:
UPDATE clients AS `Client`
INNER JOIN projects AS `Project` ON Project.client_id = Client.id
SET Client.converted = MAX(Project.last_update)
WHERE Project.converted = TRUE;
But it's not working (because I can't use MAX function directly on assignment) and I've run out of ideas on how to do an UPDATE with JOINS using the MAX function applied to a DATETIME column.
I did a SELECT statement to gather the information I need first and
it works like a charm:
SELECT Client.id, count(*), MAX(Project.last_update) FROM projects AS `Project`
LEFT JOIN clients AS `Client` ON Client.id = Project.client_id
WHERE Project.converted = TRUE
GROUP BY Client.id;
Any help is very much appreciated!
Thanks in advance.
MAX is an aggregate function, which means it cannot (or rather, generally should not) be used without a GROUP BY; you'll need to use a subquery.
UPDATE clients AS `Client`
INNER JOIN (SELECT client_id, MAX(last_update) AS max_lu
FROM projects
WHERE converted = TRUE
GROUP BY client_id
) AS `Project` ON Project.client_id = Client.id
SET Client.converted = Project.max_lu
;

SQL Update Table confusion?

I have two tables, communication(that has a column timestamp,FromIDNumber and FromX ) and commLocation(that has a column timestamp,FromIDNumber and x).
I want to set communication.FromX = commLocation.x when the two tables have the same timestamp and FromIDNumber.
I tried doing this:
UPDATE communication
SET FromX=(SELECT commLocation.x
FROM commLocation
JOIN communication
ON communication.Timestamp=commLocation.timestamp
WHERE communication.FromIDNumber=commLocation.FromIDNumber);
But I got an error:
You can't specify target table 'communication' for update in FROM clause
How would I accomplish this? This is the SQL Fiddle for it. Right now, I'm inserting the FromX values at the end of the table, and I don't want that because I need it to correspond to a certain row of communication.... how would I do this?
Any help would be greatly appreciated, thanks!!
You can do a JOIN operation in an UPDATE statement.
For example:
UPDATE communication c
JOIN commLocation l
ON l.timestamp = c.timestamp
AND l.fromidnumber = c.fromidnumber
SET c.fromx = l.x
This assumes that (timestamp,fromidnumber) is unique in commLocation.
For developing a query like this, we usually start with a SELECT statement...
SELECT c.timestamp
, c.fromidnumber
, c.fromx AS old_fromx
, l.x AS new_fromx
FROM communication c
JOIN commLocation l
ON l.timestamp = c.timestamp
AND l.fromidnumber = c.fromidnumber
The new_fromx in the SELECT is the expression/value we're going to assign to the column. We can return additional columns in the SELECT list, to verify the statement is doing what we want it to do.
Once we get that working, we convert that into an update by removing the SELECT ... FROM and replacing it with UPDATE. And adding a SET clause before the WHERE clause.

UPDATE SELECT Access

This is an MS Access 2010 related question.
Is it possible to go the short route (A) and write an UPDATE statement using a SELECT statement in order to catch the relevant value or do I have to go the long route (B) and I will firstly need to query the data through a SELECT statement that I will save as a query and then refer to this saved query in my UPDATE statement?
Here is (A):
UPDATE tbl_A
SET tbl_A.Header1 = (SELECT F1 FROM tblStaging
WHERE tblStaging.F1 = 'ISSUER CODE')
WHERE (((tbl_A.TableName)='tblStaging'));
Here is B:
SELECT F1
FROM tblStaging
WHERE F1 = 'ISSUER CODE';
UPDATE tbl_A, Q_A_Sel_ISSUERCODE
SET tbl_A.Header1 = [Q_A_Sel_ISSUERCODE].[F1]
WHERE (((tbl_A.TableName)='tblStaging'));
Thank you
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Chris gave the solution:
UPDATE tbl_A, tblStaging
SET tbl_A.Header1 = tblStaging.F1
WHERE (((tblStaging.F1)='ISSUER CODE') AND ((tbl_A.TableName)='tblStaging'));
I'm not sure about Access, but in SQL Server you can use a single UPDATE statement like this (and I would've thought it should also work in Access):
UPDATE A
SET Header1 = S.F1
FROM tbl_A A, tblStaging S
WHERE S.F1 = 'ISSUER CODE' AND A.TableName ='tblStaging';
Although if that's exactly what you want to do, it's the same as:
UPDATE tbl_A SET Header1 = 'ISSUER CODE'
WHERE TableName = 'tblStaging';

how to update one table using data from second table

I am bit stuck with this one.. what i want is update app_name (first table). It returns no error.. but does nothing...
UPDATE tbl_m_app AS tma, tbl_measure AS tm
SET tma.app_name='Ap1'
WHERE (tm.mesure_id = tma.mesure_id
AND tm.live = 1)
This query will do the same work in more obvious way and without joins
UPDATE tbl_m_app AS tma
SET tma.app_name='Ap1'
WHERE tma.mesure_id IN (SELECT tm.mesure_id FROM tbl_measure AS tm WHERE tm.live = 1)
I think this SQL is fine, it's just not matching any rows.
Check this by using a select with the same where clause:
SELECT * FROM tbl_measure tm WHERE tm.live=1;
0 rows returned, right?

MySQL, Why doesn't my update query work?

I'm made a copy of my MySQL database in MS Access as I was sure my query would work.
Heres my query
UPDATE Pads RIGHT JOIN Fix ON Pads.PadID = Fix.PadID
SET Pads.RemoveMeDate = '1999-01-01 00:00:00'
This query work in MS Access, but not in MySQL.
How do I fix this ? and why doesn't it work ?
EDIT
* When I say my query doesn't work, I mean no rows affected, when there are matching records ... *
I don't see a need for that join?
Try something like this:
UPDATE Pads
SET Pads.RemoveMeDate = '1999-01-01 00:00:00'
WHERE Pards.PadId IN (
SELECT PadId FROM Fix
)
UPDATE Pads, Fix
SET Pads.RemoveMeDate = '1999-01-01 00:00:00'
WHERE Pads.PadID = Fix.PadID
or solution above / below from Nanne depending what is a reason for JOIN
Try putting Pads.PadID = Fix.PadID in parenthesis
(Pads.PadID = Fix.PadID)
I've never actually tried doing a join on an update query, so I'm not sure if that will work.