what's wrong with this SQL code - mysql

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'

Related

Issue with a mysql query going from MySQL to MariaDB

I'm currently porting a website from PHP 5 using old mysql functions, so I basically started by replacing all of the mysql functions with the new mysqli ones and instantly got rid of most of the issues. The thing is, there is a mysql query that doesn't work anymore and I don't understand why, here is what it looks like:
SELECT *
FROM {
OJ `tableA`.`tableA`
LEFT OUTER JOIN `tableB`.`tableB` ON `tableA`.`idA` = `tableB`.`idA`
}
LEFT JOIN tableC ON tableC.idC = tableB.idC
LEFT JOIN tableD ON tableD.idD = tableC.idC
WHERE something in ('tableA','tableB')
ORDER BY column1, column2
Error says:
"Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LEFT JOIN tableC ON tableC .idC = tableB.idC' at line 6"
I want to say that I did not make the website nor I know who made it, I'm just in charge of porting it to the newer version of PHP. So I can't say for sure what this query is about, but I didn't think I would encounter such problem.
Also, I'm not familiar with this OJ {} writing so I'm not sure how I could replace it in case it was the issue here.
OPs fix for MariaDB was:
SELECT *
FROM tableA
LEFT OUTER JOIN tableB.tableB ON tableA.idA = tableB.idA
LEFT JOIN tableC ON tableC.idC = tableB.idC
LEFT JOIN tableD ON tableD.idD = tableC.idC
WHERE something in ('tableA','tableB')
ORDER BY column1, column2
Ok I fixed it by removing the curly braces and OJ and writting simply FROM tableA .... – Simon 13 mins ago
The MariaDB parser seems to only have a single table_ref in the braces.
The MySQL manual and also parser has a boarder definition.
If you which for MariaDB to support the wider format you can create a bug report

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.

Syntax error in MySQL Join Query

I'm getting a syntax error in MySQL query. Is MySQL and SQL server work differently? Can anyone suggest, what is wrong and where ?
select b.component, d.matter, d.bug, d.timestamp, d.os
from bugs.profiles p, ops_reports.BPR_TAG_DATA d
left join (Select * from bugs where product='test') b
on d.bug=b.bug_id
where d.tagid = 6
and timestamp between "2014-04-21" and "2014-04-24"
and login_name like 'test'
and p.userid = d.user
Error Message 24/04/2014 23:14:10 0:00:00.037 MySQL Database 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 'Select * from bugs where product='Conversions') as b
on (d.bu 1 0
You should not mix implicit and explicit joins. A simple rule: just don't use commas in the from clause.
select b.component, d.matter, d.bug, d.timestamp, d.os
from ops_reports.BPR_TAG_DATA d left join
bugs b
on b.product = 'test' and d.bug = b.bug_id left join
bugs.profiles p
on p.userid = d.user
where d.tagid = 6 and
timestamp between '2014-04-21' and '2014-04-24' and
login_name like 'test';
I also removed the subquery, moving the condition to the on clause. This makes the query more efficient. And changed the delimiters for the date constants to single quotes. Using double quotes for strings can lead to confusion.
EDIT:
All this said, the query in the question looks like it is syntactically correct. I notice that the error message does not refer to this exact query. The query has product='test') b and the error message has product='Conversions') as b. Perhaps there are other differences as well.

Rails find_by_sql and parameter for id

Qucik question, why is the 2nd line of code working and the first not?
Detail.find_by_sql("SELECT * FROM details INNER JOIN players ON players.id = details.player_id WHERE players.team_id = ?", self.id)
Detail.find_by_sql("SELECT * FROM details INNER JOIN players ON players.id = details.player_id WHERE players.team_id = '#{self.id}'")
first line gives me MySQL error, looks like it doesn't pass the parameter to the SQL
Mysql2::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 '?' at line 1: SELECT * FROM details INNER JOIN players ON players.id = details.player_id WHERE players.team_id = ?
You have to use it like this :
Detail.find_by_sql(["SELECT * FROM details INNER JOIN players ON players.id = details.player_id WHERE players.team_id = ?", self.id])
Wanted to comment on AshwinKumarS's answer, but seems I have no rep points, so I have to post here. He gave the right answer, but with no explanation. Yes, find_by_sql only accepts a single array as its parameter. If you look at the API doc, it gives a wrong method signature (showing the sql parameter as independent of the bind array, but the samples below it are correct). I just wasted, like, an hour, trying to debug a problem caused by this (and of course it shows up as a MySQL syntax error rather than a Rails error - very confusing and frustrating!) Because this method only accepts a single, flat array, if you have many, or a variable number of parameters to pass, do it like this:
Detail.find_by_sql(["SELECT * FROM blah WHERE column1 = ? AND column2 in (?,?,?)", array_of_values].flatten)
find_by_sql supports only sql queries while you are using rails syntax
You should write your query in sql syntax like this:
Detail.find_by_sql("SELECT * FROM details INNER JOIN players ON players.id = details.player_id WHERE players.team_id = #{self.id}")
Or, you can also find it in rails syntax:
Detail.joins(:player).where("players.team_id = ?", self.id)

MySQL UPDATE problem

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.