Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update question

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
US
So, I have this code

Code:
UPDATE pcmover.Licenses
SET NumApproved='1'
FROM pcmover.Licenses L LEFT OUTER JOIN pcmover.Uses U
  ON L.ID = U.LicenseID
WHERE L.NumApproved='2'
AND U.LicenseID IS NULL

which gives me error 1064 on line 3.
Code:
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 'FROM pcmover.Licenses L LEFT JOIN pcmover.Uses U ON L.ID = U.LicenseID
WHERE L.' at line 3
And I don't see the issue. Anybody out there with any ideas?

Thanks,
Willie
 
If it is any help, this query works fine
Code:
select count(KeyCode)
FROM pcmover.Licenses L LEFT JOIN pcmover.Uses U ON L.ID = U.LicenseId
WHERE L.NumApproved='2'
AND U.LicenseId IS NULL
and this one doesn't
Code:
UPDATE pcmover.Licenses
SET NumApproved='1'
FROM pcmover.Licenses L LEFT JOIN pcmover.Uses U ON L.ID = U.LicenseId
WHERE L.NumApproved='2'
AND U.LicenseId IS NULL
with the error
Code:
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 'FROM pcmover.Licenses L LEFT JOIN pcmover.Uses U ON L.ID = U.LicenseId
WHERE L.' at line 3
 
UPDATE L
SET NumApproved='1'
FROM pcmover.Licenses L LEFT OUTER JOIN pcmover.Uses U
ON L.ID = U.LicenseID
WHERE L.NumApproved='2'
AND U.LicenseID IS NULL
 
I still get the same error in the same place. Still confused...
 
Ok, just a SWAG - what if you remove OUTER keyword from your update statement?

The update query itself looks perfectly normal to me from ANSI SQL point of view.
 
Yup, tried it with and without outer. The odd thing is that if I just do a select rather than an update, it works fine.
 
the error message is telling you exactly where the problem is

you can't say UPDATE FROM -- the FROM is not valid syntax

check da manual for the valid syntax

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Should do it like this, moving some lines around.
[tt]
UPDATE pcmover.Licenses L
LEFT OUTER JOIN pcmover.Uses U
ON L.ID = U.LicenseID
SET NumApproved='1'
WHERE L.NumApproved='2'
AND U.LicenseID IS NULL
;[/tt]
 
Yup, that was it, thank you. Then, I moved on to the next set of updates,a little more involved (not much, but there is an aggregate column in the selection criteria). So, the idea behind the code is this
Code:
UPDATE pcmover.Licenses L
JOIN pcmover.Uses U
ON L.ID = U.LicenseID
SET NumApproved='1'
WHERE L.NumApproved='2'
HAVING count(U.LicenseID) = '1'
I want to update every license that has 2 uses approved but has only been used one time. I can quickly select those, but when I try the update, I get
Code:
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 'HAVING count(U.LicenseID) = '1'' at line 6
But if I remove the Having, then I don't know how many times the SN has been used and I can't decrease the numapproved to 1 if it has been used twice.

Any more thoughts?

Willie
 
Maybe this? or something along this line.
[tt]
SET @v:=(SELECT count(U.LicenseID) FROM pcmover.Uses U INNER JOIN pcmover.Licenses L ON L.ID=U.LicenseId AND L.NumApproved='2' GROUP BY U.LicenseID HAVING count(U.LicenseID)=1 LIMIT 1);

UPDATE pcmover.Licenses L
JOIN pcmover.Uses U
ON L.ID = U.LicenseID
SET NumApproved='1'
WHERE L.NumApproved='2' AND @v IS NOT NULL
;
[/tt]
 
Thinking more about it, maybe that's not restrictive enough.
 
Okay, this is what I can contribute on the idea and after I won't bother you. It consists on looping this block until @v is null.
[tt]
-- loop this block until @v is null
SET @v:=(SELECT [blue]U.LicenseID[/blue] FROM pcmover.Uses U INNER JOIN pcmover.Licenses L ON L.ID=U.LicenseId AND L.NumApproved='2' GROUP BY U.LicenseID HAVING count(U.LicenseID)=1 LIMIT 1);

UPDATE pcmover.Licenses L
JOIN pcmover.Uses U
ON L.ID = U.LicenseID
SET NumApproved='1'
WHERE L.NumApproved='2' AND [blue]U.LicenseID=@v[/blue]
;
[/tt]
 
One of the difficulty is to get the comma delimited list of U.LicensID or L.ID. I now have the resolution of it through group_concat() and can propose this.
[tt]
SET @v:=(SELECT group_concat(X.Y) FROM (SELECT U.LicenseID Y FROM pcmover.Uses U INNER JOIN pcmover.Licenses L ON L.ID=U.LicenseId AND L.NumApproved=2 GROUP BY U.LicenseID HAVING count(U.LicenseID)=1) X);

SET @s:=concat("UPDATE pcmover.Licenses L JOIN pcmover.Uses U ON L.ID = U.LicenseID SET NumApproved=1 WHERE L.NumApproved=2 and U.LicenseID IN (",@v,")");

PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stml;
[/tt]
 
Please forgive my ignorance, but where/how would I run something like this?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top