SQL to select a single random row with a where condition - mysql

I am trying to get a single random row from all the rows that satisfy the where clause in the SQL. I am using the following SQL but getting the corresponding error.
SELECT * FROM xyz WHERE (long='0' AND lat='0') ORDER BY RAND() LIMIT 1
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 'long='0' AND lat='0') ORDER BY RAND() LIMIT 1' at line 1

LONG is Reserved Words
SELECT * FROM xyz WHERE (`long`='0' AND lat='0') ORDER BY RAND() LIMIT 1

SELECT *
FROM ( SELECT *
FROM xyz
WHERE (`long`='0' AND lat='0')
ORDER BY DBMS_RANDOM.VALUE) LIMIT 1

Related

SubQuery and ROWNUM in MySQL : SELECT * FROM (SELECT...)

EDIT: This solved my problem:
select id_t,punctaj
from test
where punctaj in (select punctaj from test )
order by punctaj desc
limit 1;*
I have written a working Oracle SQL code but when I'm trying to convert it to MySQL I have a syntax error which I cannot solve. It looks like MySQL does not accept ROWNUM and also SELECT * FROM a subquery. What could be the solution? I need the biggest value of "PUNCTAJ" from the tests and I need to keep that "IN". Thanks!
Here is my code:
select* from (
select id_t,punctaj
from test
where punctaj in (select punctaj from test )
order by punctaj desc)
where rownum<=1
error:#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 'where rownum<=1 LIMIT 0, 25' at line 6
you have written twice the where condition,
In secondd where use AND rownum<=1

SELECT LAST(CustomerName) AS LastCustomer FROM Customers;

I am beginner to mysql, while I practice syntax of LAST() i.e
SELECT LAST(CustomerName) AS LastCustomer FROM CUSTOMERS; I am getting an error.
Error : ERROR 1064 (42000): 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 '(CustomerName) AS LastCustomer FROM
CUSTOMERS' at line 1
the thing is there is nothing on mysql like LAST() function. You can see the comment here where it is said.
You can find the list of mysql functions visiting mysql aggregated functions
It is better to use simple query like the following -
SELECT CustomerName as LastCustomer ORDER BY id DESC LIMIT 1
Hope it helps...:)
Last() is not supported in Mysql so try
SELECT TOP 1 CustomerName FROM Customers ORDER BY CustomerID DESC;
try this
SELECT * FROM CUSTOMERS ORDER BY customer_id DESC LIMIT 1;

MySQL Getting the return of a substraction

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 am trying to find last inserted record in train_stations table

SET #v100 :=(select count(*) from train_stations); select * from train_stations limit (select #v100)-1,1;
Error:Error Code: 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 '#v100,1' at line 1
this query without user variable also not working
select * from train_stations limit (select count() from train_stations)-1,1;
Error Code: 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 count() from train_stations)-1,1' at line 1
You can't use a user defined variable in LIMIT. Rather you can use PREPARE STATEMENT. An example below
PREPARE STMT FROM
" SELECT * FROM tab LIMIT ?,1 ";
select #START := count(*) from tab;
set #START := #START - 1;
select #START;
EXECUTE STMT USING #START;
DEALLOCATE PREPARE STMT;
A demo fiddle here http://sqlfiddle.com/#!8/ae474/11
EDIT:
Essentially you are trying to fetch the last record in your table which you can easily do using ORDER BY clause like
select * from train_stations order by some_id desc limit 1
Why don't you put an id column or timestamp column in your table? There is no such thing as a "natural order" of rows in MySQL, which you try to rely on.
Then all the magic would collaps to
select * from train_stations order by id desc limit 1;
or
select * from train_stations order by insert_tmstmp desc limit 1;
EDIT: The big problem with your approach is that MySQL must go through every row of train_stations just to throw them away and keep only the last one. It is better just to tell MySQL that you want directly this single row.

SELECT * FROM tablename ORDER BY LIMIT not working

i wanted to display the table values when the user selecting range. The user input saving using String range=request.getParameter();
I put this value in to this mysql query
"SELECT * FROM user_info ORDER BY user_id LIMIT '"+range+"',10";
but it displays a syntax error msg. some problem in '"+range+"' syntax.
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ''1',10' at line 1 .
You are using quotes for range. So query becomes:
SELECT * FROM user_info ORDER BY user_id LIMIT '1',10
which is wrong.
Try this:
"SELECT * FROM user_info ORDER BY user_id LIMIT "+range+",10";