I have the follwoing mysql table called user
user email count ranking
sss sss#gmail.com 111 0
ss ss#ggmail.con 11 0
s s#gmai.com 1 0
I try to use follwing mysql qyery to update the ranking
SET #r=0; UPDATE table user SET ranking= #r:= (#r+1) ORDER BY count ASC;
but it give me errors, I don't know where I did wrong, any one could help me with that? thanks a lot!
errors:
SQL query:
UPDATE TABLE user SET ranking = #r := ( #r +1 ) ORDER BY count ASC ;
MySQL said:
#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 'table user SET ranking= #r:= (#r+1) ORDER BY count ASC' at line 1
TABLE is a MySQL reserved keyword. Enclose it in backquotes when using it as an identifier, but in this case, it is unnecessary and should be removed.
SET #r=0; UPDATE user SET ranking= #r:= (#r+1) ORDER BY count ASC;
Note that 99% of the time, the error message will point exactly to the character or word in the query causing problems. Look to the first word after the ' in the error to start narrowing your problem.
> for the right syntax to use near 'table
Related
My query is:
SELECT *,
ROW_NUMBER() OVER (ORDER BY score ASC)
FROM submissions
The error message I receive is:
#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 '(ORDER BY score ASC) FROM submissions LIMIT 0, 25' at line 2
I am running this query in phpMyAdmin. I notice that OVER is not colored blue, nor does is it suggested as I type, unlike other command words (ORDER, ASC, etc).
This simpler query runs just fine:
SELECT * FROM submissions
I've tried putting things in quotes, using the RANK function instead, and fiddling with whitespace, but the query still doesn't run. What is wrong here?
My guess is that you are running a version of MySQL which is earlier than 8+, one which does not support ROW_NUMBER. There are a few options for simulating ROW_NUMBER in earlier versions of MySQL. One is to use user variables:
SELECT *,
(#row_number:=#row_number + 1) AS rn
FROM submissions, (SELECT #row_number := 0) tmp
ORDER BY score;
SELECT (SUM(MAX(identifier)+1) FROM student_ids WHERE state ='state' AND year='year');
SELECT (SUM(MAX(identifier)+1) FROM student_ids WHERE state ='state' AND year='year')
LIMIT 0, 25
MySQL said: Documentation
#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 student_ids WHERE state ='state' AND year='year')
LIMIT 0, 25' at line 1
how can i fix the error
The SUM function is for taking aggregates of columns, across multiple rows. You don't need to use it to add a scalar value:
SELECT MAX(identifier) + 1 AS max_id
FROM student_ids
WHERE state = 'state' AND year = 'year';
Additionnaly to #tim-biegeleisen, use ` for column names and table names, like this : `year`, `state`, ...
Because some words you uses are reserved. YEAR is reserved word and could return an error if you uses it as column name. SQL thinks you are calling the year function instead of a column named year.
SELECT MAX(`identifier`) + 1 AS `max_id`
FROM `student_ids`
WHERE `state` = 'state'
AND `year` = 'year' ;
Here is documentation about reserved words in mySQL :
https://dev.mysql.com/doc/refman/5.5/en/keywords.html
We are trying to get the length of a physical exercise by using the timestamps on our sensor data.
We currently have the following query:
SELECT UNIX_TIMESTAMP(
SELECT HAAS2.trainingsdata.timestamp
FROM HAAS2.trainingdata
WHERE HAAS2.trainingsdata.training_id= 1
ORDER BY timestamp DESC LIMIT 1)
- UNIX_TIMESTAMP(
SELECT HAAS2.trainingsdata.timestamp
FROM HAAS2.trainingdata
WHERE HAAS2.trainingsdata.training_id= 1
ORDER BY timestamp ASC LIMIT 1)
AS output
(enters added for readability)
When testing this query in phpMyAdmin we get the following 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 'SELECT HAAS2.trainingsdata.timestamp FROM HAAS2.trainingdata
WHERE HAAS2.trainin' at line 1
We've tried different ways to write down the query all resulting in the same error. We don't understand where the syntax error lies.
SELECT max(UNIX_TIMESTAMP(timestamp)) -
min(UNIX_TIMESTAMP(timestamp)) AS output
FROM HAAS2.trainingdata
WHERE training_id = 1
I'm used to running on an Oracle database, so I'm not really quite sure how to trouble shoot this problem. I've narrowed down a simple example of my query to the following:
SELECT 0 as gm_rowID,
'-ALL Grantmakers-' as grantmakerName
FROM dual
GROUP BY 2
phpMyAdmin runs the SQL with the following 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 'ORDER BY 2 LIMIT 0, 30' at line 1
Oracle can run this query just fine. MySQL can run the query without the GROUP BY clause. Any ideas?
--Here is the entire query:
SELECT
p.grantmaker_rowid as gm_rowID,
gm.grantmaker_companyName as grantmakerName
FROM grantmaker_info gm, proposal_submission p
WHERE 0=0
AND p.grantmaker_rowid = gm.grantmaker_rowid
UNION
SELECT
0 as gm_rowID,
'-ALL Grantmakers-' as grantmakerName
FROM dual
ORDER BY 2
GROUP BY 2
LIMIT 0 , 30
Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using column
names, column aliases, or column positions. Column positions are
integers and begin with 1
From: http://dev.mysql.com/doc/refman/5.0/en/select.html
Unless you only have 1 column in that table, it should run fine. My suggestion however would be to reference the column name (or alias) of whatever you're trying to GROUP BY.
edit: My only other suggestion is to include the SHOW CREATE TABLE output for that table.
edit2: Ok I see you've updated your question. Why not instead of ORDER BY 2, you ORDER BY grantmakerName (if that's the column you want to order by?)
Ok I've seen many similar questions but crawling over the answers couldn't make my trigger error free!
Result I need is: Whenever a new value is inserted in the database table temp_pool, it triggers and if the new address is not equal to the previous address value with the same dev_id as that of this NEW.dev_id insert the new values to location table.
Here is the query (sample):
CREATE TRIGGER filter
after insert on geo.temp_pool
for each row
BEGIN
DECLARE OLD_ADDR VARCHAR(2048);
OLD_AADR = select address from temp_pool where dev_id like NEW.dev_id
order by date desc, time desc limit 1;
IF (OLD_ADDR != NEW.address) THEN
INSERT INTO a3380361_geo.location
VALUES (NEW.dev_id,NEW.address,NEW.lat,NEW.lng,NEW.date,NEW.time);
END IF;
END
$$
I am using the phpMyAdmin editor and set the delimiter to $$.
The error that I am getting is:
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 address from temp_pool where 1 order by date desc, time desc limit 1; ' at line 5
I strongly believe that there is some problem with assigning values from SELECT to a variable [OLD_ADDR], so is there any way to solve my issue?
The logic is simple and the requirement is understandable from the query, right?
Open to all opinions and suggestions.
Instead of:
OLD_AADR = select address from temp_pool where dev_id like NEW.dev_id
Use:
SET OLD_AADR = (select address
from temp_pool
where dev_id like NEW.dev_id
order by address
limit 1);
Or using the non-standard SELECT assignment statement(not sure whether mysql supports it or not):
SELECT OLD_AADR = address from temp_pool where dev_id like NEW.dev_id
order by address
limit 1;
Not that in both cases the SELECT statement has to return only a scalar value. Thats why I used LIMIT 1.
Did you notice the typo?
OLD_ADDR OLD_AADR