MySQL UPDATE problem - mysql

I know the following MySQL code is not correct can some help me fix this code to use both tables I'm trying to grab the id from learned_skills and skill_id from users_skills for AND skill_id = id
Here is the MySQL code.
SELECT learned_skills.*, users_skills.*
UPDATE learned_skills
SET skill = '$skill', experience = '$experience', years = '$years'
WHERE user_id = '$user_id'
AND skill_id = id
Here is the error I get
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 'UPDATE learned_skills SET skill = 'some_skill', experience = '1 - 2 years' at line 2

You can perform UPDATE operations covering multiple by using the JOIN syntax, as in the following example:
UPDATE learned_skills ls
JOIN user_skills us ON (us.skill_id = ls.id)
SET skill = '$skill',
experience = '$experience',
years = '$years'
WHERE us.user_id = '$user_id';

The problem is the select statement on the first line. Remove it and run it seperately and see if that works. Also, qualify both of your tables in a join the way Daniel Vassalo suggested. Otherwise it wont know where to get half the columns from

Have a look at UPDATE Syntax and see the first user comment for help.

Related

MySQL syntax error in my code that I can't find manually

Joining two tables to get correct information and update one value from one table by conditioning with some specific conditions
SQL say that it has syntax error at line 5 although it didn't fix the syntax error automatically if he (SQL) knew that
UPDATE
sale
SET
amount = 10000
FROM
sale
JOIN delivery ON delivery.sale_id = sale.id
WHERE
sale.`status` = "active"
AND delivery.services_id = 7;
You have an error in your SQL syntax; it seems the error is around:
'FROM sale JOIN delivery ON delivery.sale_id = sale.id WHERE
sale.status ' at line 5
The correct syntax for a MySQL multi-table update (manual) puts the table references at the beginning of the query:
UPDATE
sale
JOIN delivery ON delivery.sale_id = sale.id
SET
amount = 10000
WHERE
sale.`status` = "active"
AND delivery.services_id = 7;
Demo on dbfiddle
MySQL doesn't support UPDATE ... FROM (that is a SQL Server feature)
Here is the reference to mySQL update syntax:
https://dev.mysql.com/doc/refman/8.0/en/update.html

UPDATE USING PHPMYADMIN

Good Afternoon,
I am trying to update a table using price data from another table however I get an error using an inner join. I am sure its something very stupid but having spent the best part of my day on this its time to ask for help.
If I do the following SELECT statement to test my inner join syntax works as it should
SELECT *
FROM polaracc_osrs_property_field_value
INNER JOIN polaracc_osrs_properties
ON polaracc_osrs_property_field_value.pro_id = polaracc_osrs_properties.id
WHERE polaracc_osrs_property_field_value.field_id =112
However when I then try and run an update statement using the price from one table to populate the 2nd I get the below error
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 polaracc_osrs_property_field_value INNER JOIN
polaracc_osrs_properties ' at line 3
The syntax used for the update statement is below
UPDATE polaracc_osrs_property_field_value
SET polaracc_osrs_property_field_value.value_integer = polaracc_osrs_properties.price
FROM polaracc_osrs_property_field_value
INNER JOIN polaracc_osrs_properties
ON polaracc_osrs_property_field_value.pro_id = polaracc_osrs_properties.id
WHERE polaracc_osrs_property_field_value.field_id = 112
Your join needs to happen before you set your values like this:
UPDATE polaracc_osrs_property_field_value
INNER JOIN polaracc_osrs_properties
ON polaracc_osrs_property_field_value.pro_id = polaracc_osrs_properties.id
SET polaracc_osrs_property_field_value.value_integer = polaracc_osrs_properties.price
WHERE polaracc_osrs_property_field_value.field_id = 112;
Hope this helps.

SQL syntax error with UPDATE ... FROM ... JOIN

I'm learning SQL and have built up a little database so far. Now I have 2 tables called countries_visited and regions_visited with the following schema,
countries_visited: [id,countries,year_visited]
regions_visited: [r_id,regions,c_id,month_visited]
The id column in countries_visited is a primary key, and the c_id column of regions_visited is a foreign key linked to countries_visited
The month_visited column in regions_visited is newly created and all entries are now NULL. What I want to do is to individually insert the entry into this new column with conditions.
So, I tried the following statement taking from the earlier examples here in StackOverflow,
UPDATE regions_visited
SET month_visited = 9
FROM countries_visited
JOIN regions_visited ON countries_visited.id = regions_visited.c_id
WHERE regions_visited.regions = 'Seoul'
AND countries_visited.year_visited = 2006
AND countries_visited.countries = 'South Korea';
By running this command on MySQL server, it shows this error:
ERROR 1064 (42000): 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 regions_visited INNER JOIN countries_visited
ON regions_visited.c_id = coun' at line 1
However, if neglecting the UPDATE and SET part of the statement, mysql shows the joined table with only 1 row that I would like to update. What did I do wrong? How to make this work?
Thank you in advance!
In MySQL, the join clauses come before the setclause:
UPDATE regions_visited rv
JOIN countries_visited cv ON ON cv.id = rv.c_id
SET month_visited = 9
WHERE rv.regions = 'Seoul' AND
cv.year_visited = 2006 AND
cv.countries = 'South Korea';
Move the Join before SET. Try this
UPDATE regions_visited
JOIN regions_visited
ON countries_visited.id = regions_visited.c_id
SET month_visited = 9
WHERE regions_visited.regions = 'Seoul'
AND countries_visited.year_visited = 2006
AND countries_visited.countries = 'South Korea';

what's wrong with this SQL code

I'd like to update all the rows of column URL to Test but I get the following error from the query below
#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 STx AS a LEFT JOIN Routes AS b ON a.RouteID = b.RouteID WHERE a.GroupID ' at line 3
UPDATE Routes SET URL = 'test'
WHERE ID in (
SELECT b.ID
FROM Stx a left JOIN Routes b on a.RouteID = b.RouteID
where a.GroupID = 39 and a.Status = 'Provisioned'
);
There's no syntax error here that I can see. I tested it on MySQL 5.5 and the statement parses fine.
I suspect you might have a non-ASCII whitespace character between b.ID and FROM. Try deleting all the spaces and newlines between those two tokens and then re-insert a plain space.
But that doesn't fix the next problem: MySQL doesn't support UPDATE of a table and SELECT from the same table in a single query. So you can't use a subquery in the way you're dong. That's why other answers are suggesting using a multi-table UPDATE.
Another possibility is that you're not sharing the real query you're running. A lot of people on Stack Overflow ask for help with a query, but they have modified the query to post in their question, to make it simpler or to hide proprietary information.
Please don't just say "it doesn't work." That doesn't help us improve our answers. Give the error message, if any, and be sure to show exactly the statement you're typing.
Why not just:
UPDATE Routes a JOIN Stx b ON (a.routeid = b.routeid)
SET a.URL = 'test'
where b.groupid = 39 and b.status = 'Provisioned'
I created an example SQL Fiddle here.
If you are trying to do something a bit different, can you please either post your real query, or make changes to the data model in the SQL fiddle to show the trouble you are having, and post a link to that.
UPDATE Routes AS b
JOIN Stx AS a ON a.RouteID = b.RouteID
SET b.URL = 'test'
WHERE a.GroupID = 39 and a.Status = 'Provisioned'

mySql Error in Query

I have (3) tables: Professor, Comment, Course
I'm trying to get a few columns to display the professor with all the courses and all the comments they have.
My tables
SQL:
SELECT prefix, code, info, date
FROM Course, Comment
JOIN Professor ON Professor.pID = Comment.pID
AND JOIN Course ON Course.cID = Comment.cID
WHERE Comment.pID = ?
Throws error:
Syntax error or access violation: 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 'JOIN Course ON Course.cID = Comment.cID WHERE Comment.pID = '273'' at line 4
SELECT prefix, code, info, date
FROM Comment
JOIN Professor ON Professor.pID = Comment.pID
JOIN Course ON Course.cID = Comment.cID
WHERE Comment.pID = ?
Just remove the AND before the second JOIN and remove Course in the From list. You may need to change the order of the tables depending on what table data you are primarily after.
Advise that you have a read through the MySQL JOIN syntax page.
change 'AND JOIN' to just 'JOIN'
There is no AND JOIN. Please read about JOIN syntax in the manual before attempting to use it.
Additionally, you reference table Course twice.