SQL USE INDEX Syntax Error - mysql

I am using this line on my MySQL database:
SELECT * FROM `dump` USE INDEX `time_desc` WHERE (`nodeId`=10047 AND `time`<=1377040709 AND `valid`=1) ORDER BY `time` DESC LIMIT 1;
I cannot figure out why, but the database is returning the following error:
[Error: ER_PARSE_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 'time_desc WHERE nodeId=10048 AND
time<=1377040709 AND valid=1 ORDER BY `ti' at line 1]
Any thoughts as to how I can fix this?

You need parentheses around the index name
SELECT *
FROM `dump` USE INDEX (`time_desc`)
WHERE (`nodeId`=10047 AND `time`<=1377040709 AND `valid`=1)
ORDER BY `time` DESC
LIMIT 1;

Try like this:-
SELECT * FROM `dump` USE INDEX (`time_desc`) --Use paranthesis here
WHERE (`nodeId`=10047 AND `time`<=1377040709 AND `valid`=1)
ORDER BY `time` DESC
LIMIT 1;
Check this for reference.

Related

ERROR 1064 (42000): You have an error in your SQL syntax; 'DESC'

Hello, I have the following problem, I use the world.sql schema and I can't display my query in a descending way, I get the following 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 'DESC'
I can't find a way to solve the problem other than storing the result in a table and then applying DESC. Could someone guide me what I could do or where I could investigate?
SELECT la.language, la.Isofficial AS Isofficial ,SUM(country.Population * la.Percentage / 100) pop_tot FROM country INNER JOIN countrylanguage AS la ON la.countryCode = country.Code WHERE la.language != "Spanish" AND la.Isofficial = "T" GROUP BY la.Language DESC ;
'Prior to MySQL 8.0.13, MySQL supported a nonstandard syntax extension that permitted explicit ASC or DESC designators for GROUP BY columns...As of MySQL 8.0.13, the GROUP BY extension is no longer supported: ASC or DESC designators for GROUP BY columns are not permitted.' - https://dev.mysql.com/doc/refman/8.0/en/select.html
Replace
GROUP BY la.Language DESC;
with
GROUP BY la.Language
ORDER BY la.Language DESC;
Which will work in whatever MySQL server version you have.
BTW, I've tested the first syntax and it works well in version 5.5

LIMIT in MySQL Update Row

I am trying to Update Some row in my database. If I run without limit its working fine but if I run it with limit its giving me error like 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 '35' at line 1
My Query is like below
UPDATE number_list SET sync = 0 WHERE server = 1 ORDER by id ASC LIMIT 0,35
Let me know if someone can correct me.
You can use limit in an update (in MySQL) but not an offset. So just do:
UPDATE number_list
SET sync = 0
WHERE server = 1
ORDER by id ASC
LIMIT 35;
This is a bit subtle, because SELECT supports offsets. However, it is clear in the syntax diagram for UPDATE.

Syntax Error in SQL query. Not able to find mistake

I am writing tests for my database in mysql.
con.query(“SELECT `list`.* FROM `list` WHERE `list`.`place` = 86 AND `list`.`person` = \"#{person_id}\" AND (( (list.state = 'open' AND num=\"#{num}\") || (list.state = ?) )) AND (list.updated_at > \"2018-06-05\") ORDER BY list.person, list.state='closed' DESC, list.updated_at DESC LIMIT 50 OFFSET 1”)
list is a table, and num, place, and person are values of a single item from list. I'm also using this in a ruby script.
I get a syntax error. The error message is:
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 ‘`?) )) AND (list.update_at > “2018-06-05”)) ORDER BY list.person, or`’
I cannot figure out the issue. Please help me.
Look at the ORDER BY part of the query
ORDER BY list.person, list.state='closed' DESC, list.updated_at DESC
list.state should not have an ='closed'
Just try changing the it all to
ORDER BY list.person, list.state DESC, list.updated_at DESC
I think you are using the wrong character for double quotes. You have “. Try " instead.

Display recent record

i was trying to display recent record.
i insert sql below in the recordset
SELECT `date`, `item_name`, `stock_invent`, `id_invent`,`num_in_out`
FROM `inventories` ORDER BY `id_invent` DESC LIMIT 3
But, how to make it display in table? because i got message
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 'LIMIT 0, 10' at line 1
SELECT date, item_name, stock_invent, id_invent, num_in_out
FROM inventories ORDER BY id_invent DESC LIMIT 0,3

DELETE FROM `table` AS `alias` ... WHERE `alias`.`column` ... why syntax error?

I tried this with MySQL:
DELETE FROM `contact_hostcommands_relation` AS `ContactHostCommand` WHERE (`ContactHostCommand`.`chr_id` = 999999) LIMIT 1
And I get 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 'WHERE (`ContactHostCommand`.`chr_id` = 999999) LIMIT 1' at line 1
Note: This query is automatically generated and conditions are based on table aliases.
Why I get this error?
Is there any way to use table aliases in where clause?
Is this MySQL specific?
What #Matus and #CeesTimmerman said about MSSQL, works in MySQL 5.1.73 too:
delete <alias> from <table> <alias> where <alias>.<field>...
You can use SQL like this:
DELETE FROM ContactHostCommand
USING `contact_hostcommands_relation` AS ContactHostCommand
WHERE (ContactHostCommand.`chr_id` = 999999)
LIMIT 1
You cannot use AS in a DELETE clause with MySQL :
DELETE FROM `contact_hostcommands_relation` WHERE (`chr_id` = 999999) LIMIT 1