How can i update the results of a MySQL select query - mysql

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.

Related

Update rows based on WHERE from INNER JOIN

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.

How to use JOIN in UPDATE mysql query

Hello I want to bind this to one query with JOIN.
How does this work:
Db::bind("uid", strip_tags($userid));
DB::bind("user_id", strip_tags($refer));
Db::bind("points_earn", strip_tags($points_earn));
Db::bind("points_refer", strip_tags($points_refer));
Db::query("UPDATE referrals SET `points_earn` = :points_earn WHERE new_id = :uid");
Db::query("UPDATE users SET `points` = `points` + :points_refer WHERE id = :user_id");
What I think but not work.
Db::query("UPDATE referrals r JOIN users u ON r.user_id = u.id SET `r.points_earn` = :points_earn WHERE r.new_id = :uid AND `u.points` = `u.points` + :points_refer WHERE u.id = :user_id");
Have anyone a solution?
Your syntax is right, but the back ticks are wrong and each SELECT only has one WHERE:
UPDATE referrals r JOIN
users u
ON r.user_id = u.id
SET r.points_earn = :points_earn
WHERE r.new_id = :uid AND u.points = u.points + :points_refer AND
u.id = :user_id;
The problem with backticks is that the following are quite different:
`r.points_earn`
`r`.`points_earn`
The first refers to a column called "r.points_earn". The second refers to the "points_earn" column in "r". The backticks aren't necessary so you can just remove them.

COALESCE function in MYSQL with select gives error

I am wondering what is the problem of my query which is simple for me, please also have a look and please let me know if I missed something
SELECT users.*, p.*,
COALESCE(SELECT picture_location FROM pictures
WHERE user_id = 'patient' AND default_pic = 1,
SELECT picture_location FROM pictures
WHERE user_id = 'patient' ORDER BY id ASC LIMIT 1) AS default_pic,
pic.picture_location, MONTHNAME(users.registered_on) AS month_reg,
YEAR(users.registered_on) AS year_reg FROM users
LEFT JOIN profile p ON p.profile_id = users.profile_id
LEFT JOIN pictures pic ON pic.user_id = p.profile_id
WHERE users.pseudo = 'patient' GROUP BY users.pseudo
ORDER BY pic.default_pic DESC LIMIT 1
Do you see something ? It returns me 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 'SELECT picture_location FROM pictures WHERE user_id = 'patient' AND default_pic ' at line 1
A nested select needs an additional layer of parentheses:
SELECT users.*, p.*,
COALESCE((SELECT picture_location FROM pictures WHERE user_id = 'patient' AND default_pic = 1),
(SELECT picture_location FROM pictures WHERE user_id = 'patient' ORDER BY id ASC LIMIT 1)) AS default_pic,
pic.picture_location,
MONTHNAME(users.registered_on) AS month_reg, YEAR(users.registered_on) AS year_reg
FROM users LEFT JOIN
profile p
ON p.profile_id = users.profile_id LEFT JOIN
pictures pic
ON pic.user_id = p.profile_id
WHERE users.pseudo = 'patient'
GROUP BY users.pseudo
ORDER BY pic.default_pic DESC
LIMIT 1;

MySql error in your SQL syntax for date

I have just migrated my database from MS SQL to MySQL.
When running reports I now get the following error:
Application Execution Exception
Error Type: database : 0
Error Messages: 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 '2 YEAR(res_created) AS responseYear FROM Responses INNER JOIN ' at line 1
SQL Sent:
SELECT DISTINCT TOP #arguments.yearsToReturn# YEAR(res_created) AS responseYear
FROM
Responses
INNER JOIN
Questions ON Responses.question_id = Questions.question_id
INNER JOIN
Lookup_Survey_Questions ON Questions.question_id = Lookup_Survey_Questions.question_id
INNER JOIN
Survey ON Lookup_Survey_Questions.survey_id = Survey.survey_id
INNER JOIN
School ON Survey.sch_id = School.sch_id
INNER JOIN
Authority ON School.auth_id = Authority.auth_id
WHERE
Authority.auth_id = #arguments.authorityID#
AND
Questions.question_id = #arguments.questionID#
AND
Responses.survey_id IN (SELECT Survey.survey_id FROM Survey where Survey.sch_id in (SELECT School.sch_id FROM School where auth_id=#arguments.authorityID#))
ORDER BY
responseYear ASC
How can I resolve?
Thanks
I think that this can be simplified as follows...
SELECT DISTINCT YEAR(res_created) responseYear
FROM Responses r
JOIN Questions q
ON q.question_id = r.question_id
JOIN Lookup_Survey_Questions lsq
ON lsq.question_id = q.question_id
JOIN Survey u
ON u.survey_id = lsq.survey_id
JOIN School c
ON c.sch_id = u.sch_id
JOIN Authority a
ON a.auth_id = c.auth_id
WHERE a.auth_id = #arguments.authorityID#
AND q.question_id = #arguments.questionID#
AND c.auth_id=#arguments.authorityID#
ORDER
BY responseYear ASC
LIMIT 2;
"Top 2" isn't MySQL syntax. To select the first two results add limit 2 to the end of the query:
...
ORDER BY
responseYear ASC LIMIT 2
You are using the two clause at a place. You are using the DISTINCT and TOP that's why there is error.
Use this query:
SELECT DISTINCT YEAR(res_created) AS responseYear
FROM Responses
INNER JOIN Questions ON Responses.question_id = Questions.question_id
INNER JOIN Lookup_Survey_Questions ON Questions.question_id = Lookup_Survey_Questions.question_id
INNER JOIN Survey ON Lookup_Survey_Questions.survey_id = Survey.survey_id
INNER JOIN School ON Survey.sch_id = School.sch_id INNER JOIN Authority ON School.auth_id = Authority.auth_id
WHERE Authority.auth_id = 5 AND Questions.question_id = 20 AND Responses.survey_id IN (
SELECT Survey.survey_id FROM Survey where Survey.sch_id in (SELECT School.sch_id FROM School where auth_id=5))
ORDER BY responseYear ASC
LIMIT 0,2; //use the limit

Every derived table must have its own alias

I have the following query:
SELECT `snap`.`ID`, `user`.`username`, `vote`.`type`
FROM (`snap`) JOIN `user` as u ON `u`.`ID` = `snap`.`user`
LEFT JOIN (select * from vote where user = "18") as vote ON `snap`.`ID` = `vote`.`snap`
JOIN (SELECT CEIL(MAX(ID)*RAND()) AS ID FROM snap)) AS x ON `snap`.`ID` => `x`.`ID`
WHERE `snap`.`active` = 0 LIMIT 1
It worked just fine untill I added the last JOIN. Now i get the error: "Every derived table must have its own alias". I know it is because every table needs its on alias and I need to put "as S" or something somewhere, but I can't find out how to do that in this query.
Seems like you have extra closing parenthesis after snap. It should be 1 instead of 2 closing parentheses.
SELECT `snap`.`ID`, `user`.`username`, `vote`.`type`
FROM (`snap`) JOIN `user` as u ON `u`.`ID` = `snap`.`user`
LEFT JOIN (select * from vote where user = "18") as vote ON `snap`.`ID` = `vote`.`snap`
JOIN (SELECT CEIL(MAX(ID)*RAND()) AS ID FROM snap) AS x ON `snap`.`ID` = `x`.`ID`
WHERE `snap`.`active` = 0 LIMIT 1
Correct syntax is :
SELECT `snap`.`ID`, `user`.`username`, `vote`.`type`
FROM (`snap`) JOIN `user` as u ON `u`.`ID` = `snap`.`user`
LEFT JOIN (select * from vote where user = "18") as vote ON `snap`.`ID` = `vote`.`snap`
JOIN (SELECT CEIL(MAX(ID)*RAND()) AS ID FROM snap) AS x ON `snap`.`ID` = `x`.`ID`
WHERE `snap`.`active` = 0 LIMIT 1