SELECT * FROM tablename ORDER BY LIMIT not working - mysql

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";

Related

MySQL get calculated column and all column(*)

i have a table and i have columns. I want to get AGE column- it is creating (current date - year column data)- and whole columns like *.
I tried;
SELECT (YEAR(CURRENT_DATE) - YEAR(dateOfBirth)) as age , * FROM users
but it gives 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 users LIMIT 0, 25' at line 1
What should i write? Thank you!
use alias and TIMESTAMPDIFF function
SELECT u.*,
TIMESTAMPDIFF(YEAR, dateOfBirth, CURRENT_DATE) as age
FROM users u
'Use of an unqualified * with other items in the select list may produce a parse error. To avoid this problem, use a qualified tbl_name.* reference' - https://dev.mysql.com/doc/refman/8.0/en/select.html
This is ok
SELECT (YEAR(CURRENT_DATE) - YEAR(dateOfBirth)) as age, users.* FROM users

Why do you have to count after 'select *' on MySQL?

So, today I tried doing the following:
SELECT count(users), * FROM table;
And it gave me a syntax error on the , after count(users), but this:
SELECT *, count(users) FROM TABLE;
Or this:
SELECT count(users), users FROM table;
Does work. Is there a reason for that?
Edit: Per request, here's the 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 '* from mygamedb' at line 1
MySQL Workbench underlines the comma after the count(*). I'd like to point out I know it's an ugly select, I should group by and such, but the question is not about how to make it work, but a short example asking about why does that happen, not how to make it work.
You could use alias to make it work:
SELECT count(users), t.*
FROM table t;
DBFiddle Demo
SELECT count(users), *
FROM table;
-- 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 tablez' at line 1
I am trying to explain you when you will use this SELECT count(users), * FROM table; query then mysql query fetch table as like
Table :
---count(users)---
result of count
Then you are adding * means fetch all attribute value but now mysql check * from fetch table but that is not their because table has only one attribute that name is count(users) that's why it show error. but when you will use t.table (t is alias of table) that fetch from table and it works fine.
I am commenting here what is my understanding.
Thanks

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

SQL to select a single random row with a where condition

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