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;
Related
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
The statement is like SELECT * FROM db.table group by id desc;
Would raise an error like
15:02:24 SELECT * FROM db.table group by id
desc LIMIT 0, 10 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 'desc LIMIT 0, 10' at line 1 0.00014
sec
on MySQL 8.0.13 in Ubuntu 18.04 Desktop 64bit
which would be fine on MySQL 5.7 in Windows or CentOS or Ubuntu.
I know basically, the select statement is like.
SELECT statement... [WHERE condition | GROUP BY `field_name(s)` HAVING condition] ORDER BY `field_name(s)` [ASC | DESC];
So is this 5.7's problem not to issue the error?
Or something more complicated on SQL standard?
I have the same issue, so for MySQL 8, I used the sql like that:
SELECT * FROM db.table
group by id
order by id desc
Taking from #P.Salmon's comment for the question.
If you look up the select statement in the manual
http://dev.mysql.com/doc/refman/5.7/en/select.html you will see
that up to 5.7 asc|desc are optional modifiers to the group by
statement which are no longer present from 8.0.and if you look at the
upgrade documentation
https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-sql-changes
This deprecation is documented.
Since this situation, #Linda Li's answer could be a good option.
This query makes no sense:
SELECT *
FROM db.table
GROUP BY id DESC;
You are doing an aggregation query. So (presumably), the table has multiple rows per id. Those are condensed down to one row. What values should be used for the other columns? It is sad that MySQL ever supported this syntax. So a welcome change is that ONLY_FULL_GROUP_BY is now the default.
A small hint is that using an aggregation query with no aggregation functions is suspicious.
Perhaps you want:
select id, min(col1), min(col2), . . .
from t
group by id;
Or more likely, you want a particular row, such as the "earliest" or "most recent", something like:
select t.*
from t
where t.createdAt = (select min(t2.createdAt) from t t2 where t2.id = t.id);
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 trying to run this in sql query, but not working. I tried single cast, it works fine, but when i tried to add two cast, 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 useradvert WHERE optionsalerent='For sale' ORDER BY cast (rm as signed)' at line 1
This query below works.
SELECT * FROM popol WHERE optsale='For sale' ORDER BY cast(rm as signed) ASC
THis one doesn't work
SELECT * FROM popol WHERE optsale='For sale' ORDER BY cast(rm as signed) ASC, cast(salary as signed) ASC
I need to output both rm and salary rm. Both are integer values. Tqs in advance.
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?)