UPDATE default_weekly_stats s
INNER JOIN default_profiles p
ON p.user_id = s.user_id
WHERE (default_profiles.opid = 0 OR default_profiles.opid IS NULL)
AND s.week = `1`
AND s.correct_picks = `4`
SET s.rank = 1
That's my query and I'm getting an error:
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 'WHERE (default_profiles.opid = 0 OR default_profiles.opid IS NULL) AND s.week = ' at line 1
The syntax looks right to me, but clearly I'm missing something. Any ideas what?
WHERE comes after SET
UPDATE default_weekly_stats s
INNER JOIN default_profiles p ON p.user_id = s.user_id
SET s.rank = 1
WHERE (default_profiles.opid = 0 OR default_profiles.opid IS NULL)
AND s.week = 1
AND s.correct_picks = 4
And as #Chad mentioned: Leave the backticks. You could use quotes but don't need any delimiter for numbers.
Look at the UPDATE Syntax
Try this:
UPDATE
default_weekly_stats s
INNER JOIN default_profiles p
ON p.user_id = s.user_id
SET s.rank = 1
WHERE (default_profiles.opid = 0 OR default_profiles.opid IS NULL)
AND s.week = `1`
AND s.correct_picks = `4`
Related
I need to update a column's value when a row exists based on multiple conditions within a join. I've got the following:
SELECT *
FROM Fixtures f
INNER JOIN Results r ON f.FixtureID = r.FixtureID
WHERE f.Status = 1
AND f.IsResult = 0
AND f.Season = 1
AND r.TeamID IS NOT NULL
AND (DATEDIFF(NOW(), f.Date) >= 2)
Which returns a single row that matches, I'd like to then update f.IsResult so have done the following:
UPDATE f
SET f.IsResult = 1
FROM Fixtures f
INNER JOIN Results r ON f.FixtureID = r.FixtureID
WHERE f.Status = 1
AND f.IsResult = 0
AND f.Season = 1
AND r.TeamID IS NOT NULL
AND (DATEDIFF(NOW(), f.Date) >= 2)
However I get an error when trying this #1064 - 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 Fixtures f INNER JOIN Results r ON f.FixtureID = r.FixtureID WHERE f.S' at line 3
Try this:
UPDATE Fixtures
SET Fixtures.IsResult = 1
WHERE Fixtures.Status = 1
AND Fixtures.IsResult = 0
AND Fixtures.Season = 1
AND (SELECT TeamID FROM Results WHERE Fixtures.FixtureID = Results.FixtureID) IS NOT NULL
AND (DATEDIFF(NOW(), Fixtures.Date) >= 2)
By using (Results) in a subquery, UPDATE statement has a single table (Fixtures) to target.
I got error message :
Query error: 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 'AS ChildCnt from tbl_user_master u WHERE u.sponsor_id = '145' AND u.user_type = ' at line 4 - Invalid query: select u.user_id,(SELECT COUNT(o.user_id) FROM tbl_user_master o WHERE o.sponsor_id = u.user_id AND o.user_type = 2 AND o.status =1 AND NOT EXISTS (
mysql code :
$user_tree = $this->getAllRec("u.user_id,(SELECT COUNT(o.user_id) FROM tbl_user_master o WHERE o.sponsor_id = u.user_id AND o.user_type = 2 AND o.status = 1 AND NOT EXISTS (
SELECT p.user_id
FROM tbl_flamingo_product_order p
WHERE o.user_id = p.user_id AND p.status=3) AS ChildCnt",
"tbl_user_master u",
"WHERE u.sponsor_id = '".$user_id."' AND u.user_type = 2 AND u.status =1");
May you help me find out where is the syntax error?
it is ok. I found it.
just replace
status=3)
to
status=3))
I have the following SELECT query...
SELECT users.id, users.firstname, users.lastname, users.email,
users.live, users.editoverride,
usertenderstage.tenderId,
usertenderstage.stageId, usertenderstage.statusId
FROM users
JOIN usertenderstage ON usertenderstage.userId = users.id
WHERE users.live = 1 AND usertenderstage.tenderId = 1
AND usertenderstage.stageId = 2 AND usertenderstage.statusid = 6
I am trying to edit the query to make it an UPDATE query, and set users.editoverride = 1. My attempt is below...
UPDATE users.id, users.firstname, users.lastname, users.email,
users.live, users.editoverride,
usertenderstage.tenderId,
usertenderstage.stageId, usertenderstage.statusId
FROM users
JOIN usertenderstage ON usertenderstage.userId = users.id
SET users.editOverride = 1
WHERE users.live = 1 AND usertenderstage.tenderId = 1
AND usertenderstage.stageId = 2 AND usertenderstage.statusid = 6
But I am getting error message
#1064 - 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 users
JOIN usertenderstage ON usertenderstage.userId = users.id
SET users' at line 5
Could someone please help me to know what I'm doing wrong? Thanks.
Following should work:
UPDATE users
SET editoverride = 1
WHERE id IN
(SELECT users.id
FROM users
JOIN contractorscontractsappliedfor ON contractorscontractsappliedfor.contractorId = users.id
JOIN usertenderstage ON usertenderstage.userId = users.id
WHERE users.live = 1
AND contractorscontractsappliedfor.contractid = 1
AND usertenderstage.stageId = 2
AND usertenderstage.statusid = 6)
It seems the issue is with MySQL as you are getting error "#1093 - You can't specify target table 'users' for update in FROM clause". To get around, nest it one level deep as below:
UPDATE users
SET editoverride = 1
WHERE id IN
(SELECT A.id
FROM
(SELECT users.id
FROM users
JOIN contractorscontractsappliedfor ON contractorscontractsappliedfor.contractorId = users.id
JOIN usertenderstage ON usertenderstage.userId = users.id
WHERE users.live = 1
AND contractorscontractsappliedfor.contractid = 1
AND usertenderstage.stageId = 2
AND usertenderstage.statusid = 6) AS A)
I haven't tested it as don't have MySQL handy.
UPDATE DFEntryValues
SET DFEntryValues.DFFieldvalue = NOW()
FROM DFEntryValues
JOIN DFEntries ON DFEntryValues.DFEntryID = DFEntries.DFEntryID
JOIN DynamicFormStructures ON DFEntries.DynamicFormStructureID = DynamicFormStructures.DynamicFormStructureID
JOIN Projects ON DynamicFormStructures.ProjectID = Projects.ProjectId
JOIN Clients ON Projects.ClientID = Clients.ClientID
JOIN DFFieldDefinition ON DFEntryValues.DFFieldDefinitionID = DFFieldDefinition.DFFieldDefinitionID
WHERE Clients.ClientID = '26' AND DFFieldDefinition.label = 'Geboortedatum';
I get the following error:
Error Code: 1064
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 'WHERE Clients.ClientID = '26' AND DFFieldDefinition.label = 'Geboortedatum' SET' at line 12
Can someone point me out what's wrong with this query?
Kind regards!!
It seems you are not using correct format.
Hope this helps.
UPDATE DFEntryValues JOIN DFEntries ON DFEntryValues.DFEntryID = DFEntries.DFEntryID
JOIN DynamicFormStructures ON DFEntries.DynamicFormStructureID = DynamicFormStructures.DynamicFormStructureID
JOIN Projects ON DynamicFormStructures.ProjectID = Projects.ProjectId
JOIN Clients ON Projects.ClientID = Clients.ClientID
JOIN DFFieldDefinition ON DFEntryValues.DFFieldDefinitionID = DFFieldDefinition.DFFieldDefinitionID
SET DFEntryValues.DFFieldvalue = NOW()
WHERE Clients.ClientID = '26' AND DFFieldDefinition.label = 'Geboortedatum';
I'm receiving an error on the below SQL in MySQL and i'm unsure why. I'm new to MySQL.
UPDATE I
SET I.user_comments = DD.value
FROM
redcap_user_information I
JOIN redcap_data D ON D.value = I.user_email AND D.project_id = 439 AND D.field_name = 'email'
JOIN redcap_data DD ON DD.record = D.record AND DD.project_id = 439 AND DD.field_name = 'irb'
WHERE
user_comments IS NULL
AND allow_create_db = 1
AND user_suspended_time IS NULL
ORDER BY I.user_email
The error is
Error Code: 1064. 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
redcap_user_information I
JOIN redcap_data D ON D.value = I.use'
Can anyone shed some light?
Not sure about the logic of the join part but the correct syntax for update with join would be as
update redcap_user_information I
JOIN redcap_data D ON D.value = I.user_email AND D.project_id = 439 AND D.field_name = 'email'
JOIN redcap_data DD ON DD.record = D.record AND DD.project_id = 439 AND DD.field_name = 'irb'
set I.user_comments = DD.value
WHERE
user_comments IS NULL --- use the table alias name to access the column
AND allow_create_db = 1 --- use the table alias name to access the column
AND user_suspended_time IS NULL --- use the table alias name to access the column