Query issues, for the life of me i cant figure out what is wrong with this query
HAVING distance ='10'
GROUP BY c.ancient,
c.ant_name,
p.upper,
p.name,
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 'GROUP BY c.city_id, c.city_name,
p.prop_ynow_id, p.Name, pr.PRE_Ident
SELECT dis' at line 1
HAVING goes after GROUP BY. MySQL is picky this way.
A little late to the party but Queries in any standard SQL from my experience generally have to go:
INSERT,
DELETE,
SELECT,
FROM,
WHERE,
GROUP BY,
HAVING, ORDER BY
Order.
I don't do MySQL, but in the SQL I'm used to, the HAVING clause needs to go after the GROUP BY clause.
Related
My query statement
SELECT Count(DISTINCT name)
FROM facebook.users
results to
mysql.ProgrammingError: (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 '\xef\xbb\xbfSELECT Count(DISTINCT name)
\r\nFROM facebook.users' at line 1")
How do I fix this, thanks
You should check how you pass your query. \xef\xbb\xbf is UTF-8 BOM.
The UTF-8 representation of the BOM is the (hexadecimal) byte sequence 0xEF,0xBB,0xBF
SELECT Count(DISTINCT name) FROM facebook.users
-- query is correct
I found this post:
Using an Alias in SQL Calculations
which suggests you can use an alias in calculations by using
(select alias)
like:
SELECT 10 AS my_num,
(SELECT my_num) * 5 AS another_number
FROM table
This works fine. But now I want to use an alias in an if. So I thought it might work the same way. So I tried:
SELECT 10 AS my_num,
IFNULL(otherfield, (SELECT my_num)) AS another_number
FROM table
which doesn't work at all, telling me
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 '(SELECT my_num)) AS another_number
Is there any way to make this work in MySql?
No, use the entire expression directly like below unless you are accessing it in a outer query.
IFNULL(otherfield, 10) AS another_number
In your case it should be
SELECT 10 AS my_num,
IFNULL(otherfield, 10) AS another_number
FROM table
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;
I am very new to SQL/MYSQL and I am having a problem…I created a database with tables and such and I can search it no problem…however when I add in a "WHERE" statement, I get the error code 1064. any help would be appreciated.
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 'WHERE salary =>60000' at line 1
My query is this…
SELECT
salary
FROM
instructor;
WHERE
salary => 60000
If I run the query without the "WHERE" line it works just fine, but with it i get that error above. also, just to note, the word "WHERE" IS UNDERLINED IN RED, BUT NO OTHER WORDS ARE.
thanks for any help!
also, I'm using mysql workbench version 5.2.47 on a mac
SELECT salary FROM instructor; WHERE salary =>60000
you have an added ';' before the WHERE. That could be causing the issue.
It should instead be:
SELECT salary from instructor WHERE salary => 60000;
Semicolon will terminate the SQL statement so anything typed after the semicolon is considered to be a new statement.
Since starting a query with a WHERE keyword is syntactically incorrect, the WHERE clause was underlined.
SELECT salary FROM instructor; WHERE salary => 60000
SELECT salary FROM instructor;
is a statement by itself without the where clause.
However,
WHERE salary => 60000
does not make any sense to the system hence is marked by the editor.
This question is all about laziness... I'd like to do something like this:
select some_func(some_col), * from my_table
So that I don't have to do this:
select some_func(some_col), col_1, col_2... col_ad_infinitum from my_table
Is there any way to make the first query work? This is the error I get when I run it:
ERROR 1064 (42000) at line 1: 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 my_table' at line 1
Do you mean that in MySQL your first query:
SELECT some_func(some_col), *
FROM my_table
produces this 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 '*' at line 1
You can change your code into (this results in no errors!):
SELECT *, some_func(some_col)
FROM my_table
or into this, if you want to have the calculated columns first:
SELECT some_func(some_col), t.*
FROM my_table AS t
Unfortunately, mysql only supports the asterisk at the start of the column list (unlike every other DB I am familiar with)
(Edited: start not end - oops!)
Change the order of your select params:
select *,some_func(some_col) from my_table
Anyway, as the Zen of Python says: "Explicit is better than implicit". Always try to write the fields you're selecting, and if it's posible try to put the table they're from too, you can use an alias. Your future YOU will thank you.
select t.some_col from my_table t
When I do that with PostgreSQL, I get the column(s) I specify followed by all the other columns (possibly repeating the column(s) I specified).