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?)
Related
I want to show the count in the SQL query, but I have a problem with it. I want to search for count only if there is something specific in the value column. Here is an example of my query:
SELECT COUNT(IF status='F') FROM relation WHERE from='7'
So, here I want to get the amount of "relation" from the column "status" from the table, when the status value is F.
With the above code, I get an error message:
#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 '`status`='F') FROM `relation` WHERE `from`='7' LIMIT 0, 25' at line 1
I think two common ways are:
SELECT
COUNT(CASE WHEN status='F' THEN 1 END)
FROM relation
WHERE from='7'
and
SELECT
SUM(CASE WHEN status='F' THEN 1 ELSE 0 END)
FROM relation
WHERE from='7'
The first one uses count but since it only counts non-null values, it only counts the ones you want. The second uses sum, but since we sum 1 if your condition is true and 0 if your condition is false it's equivalent to counting where the condition is true.
Although in your case since you're not doing a group by you could just use
SELECT
COUNT(*)
FROM relation
WHERE from='7' AND status='F'
You can count this way to get only counts where the value of status is F
SELECT COUNT(*) FROM relation WHERE from='7' AND status='F';
I prefer summing a boolean expression:
SELECT SUM(status = 'F') AS cnt
FROM relation
WHERE `from` = '7';
Note that FROM is a reserved MySQL keyword. If you really did name a column FROM, then you will have to forever escape it in backticks. You should avoid naming your database objects using reserved keywords.
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);
What is the difference between these two MySQL statements?
Works:
select *, count(mycol) c from mytable group by mycol;
Doesn't work:
select count(mycol) c, * from mytable group by mycol;
The first statement works as I'd expect, while the second one gives me a syntax error. Why does the order matter?
I'm having trouble finding an answer from Google, because I'm not entirely sure if I'm asking the question correctly.
Edit:
Here's the sanitized error message. I'm using MySQL Workbench, if that's relevant.
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 mytable group by id' at line 1
Just alias the table and the syntax error will go away.
select count(t.id) c, t.* from mytable t group by id;
See this db fiddle.
It looks like MySQL allows bare (unqualified) * only as immediatly following SELECT. The following query also raises a syntax error :
select 1, * from mytable t;
The documentation prevents against using bare * combined with other items in the SELECT list :
A select list consisting only of a single unqualified * can be used as shorthand to select all columns from all tables.
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.
I have MySQL Server version 5.1.53. I was looking for an hour to answer this question by myself. Including read the documentation itself at http://dev.mysql.com/doc/refman/5.1/en/select.html
Currently, I run this query.
SELECT dv2.timestamp
FROM data_val AS dv2
WHERE dv2.timestamp > '2011-06-10 22:26:25' ORDER BY dv3.timestamp DESC
LIMIT 1
Then I was trying to eliminate the ORDER BY syntax by determining the calculation of MAX_QUERIES minus 1. By doing that I could write,
SELECT (COUNT(*)-1) total
FROM data_val AS dv2a
WHERE dv2a.timestamp > '2011-06-10 22:26:13'
Finally the query becomes,
SELECT dv2.timestamp
FROM data_val AS dv2
WHERE dv2.timestamp > '2011-06-10 22:26:13'
LIMIT (
SELECT (COUNT(*)-1) total
FROM data_val AS dv2a
WHERE dv2a.timestamp > '2011-06-10 22:26:13'
), 1
And the error 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 (COUNT(*)-1) total FROM data_val AS dv2a ' at line 4
I also tried to put the subquery after OFFSET syntax. but still error.
Do you have any idea why my sub-query doesn't work?
I need technical details with short,
simple, and clean explanation.
From the MySQL manual: http://dev.mysql.com/doc/refman/5.5/en/select.html
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants, with these exceptions:
Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.
Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6.
The MySQL query optimizer needs to resolve the limit parameters to a constant before running the query, or it will not know how many rows to return.
You can't imbed a query result for a limit parameter
I need to return average for each of 12 columns I have in a table in DB. MySQL allows one to get average for one column only. The following query (for one column) works:
SELECT station_id, AVG(jan) AS avg_jan
FROM `climate_data`
WHERE element_name = "Temp_mean_mly" AND jan <> -999999
GROUP BY station_id
and the following (for multiple columns) does not (I get syntax error):
SELECT station_id, AVG(jan) AS avg_jan, AVG(feb) AS avg_feb, ... ,
AVG(dec) AS avg_dec
FROM `climate_data`
WHERE element_name = "Temp_mean_mly"
AND jan <> -999999
AND feb <> -999999
AND ...
AND dec <> -999999
GROUP BY station_id
Do I have to use 12 sub-queries to achieve the result I need?
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 'dec)
dec is a reserved MySQL keyword; changing it to `dec` in your query would probably fix that error for you :).
Edit: note that you're also using it in the WHERE clause; it might work there (as it's unlogical for MySQL to find a keyword there), but keep it in mind that you might also have to escape that one :)