select *
from ( select * from table ) 'table1';
I cannot see why I am getting this error:
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 ''table1'' at line 2
I have checked the manual (MySQL subquery in FROM clause) and I can't see any difference between the examples and my little statement.
Table names/aliases must either be surrounded with backticks or nothing
select *
from ( select * from table1 ) table1;
I think you want back quotes rather than forward quotes:
select *
from ( select * from table ) `table1`;
Forward quotes specify a string constant. Back quotes delimit a name.
when you have a subquery that is a table, you need to declare it with a name.
select * from (select * from table1) as x
As well as not putting quotes around the alias, I believe you also need backticks around "table" in the subquery because it's a reserved word in MySQL (assuming you did actually name your table "table"):
select * from ( select * from `table` ) table1;
Related
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.
The following query works:
SELECT DISTINCT `number`
FROM `employee`
WHERE `number` IN
(SELECT `department_manager`
FROM `department`)
UNION
(SELECT DISTINCT `manager`
FROM `employee`
WHERE `manager` IS NOT NULL)
But as soon as I wrap the query with parentheses it doesn't work anymore:
(SELECT DISTINCT `number`
FROM `employee`
WHERE `number` IN
(SELECT `department_manager`
FROM `department`)
UNION
(SELECT DISTINCT `manager`
FROM `employee`
WHERE `manager` IS NOT NULL))
Causing syntax error:
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 'UNION
Wrapping other select queries in parentheses doesn't cause problems, this works for example:
(SELECT DISTINCT number FROM johnson.employee);
What is the difference between these?
This has to do with the way MySQL implemented its SQL grammar.
A <query> can be:
SELECT ... [ UNION <query>]
or
( SELECT ... ) [ UNION <query> ]
But apparently not
( SELECT ... UNION <query> )
Read sql/sql_yacc.yy in the MySQL source code if you want the details.
As a workaround, you can do this:
SELECT * FROM (SELECT ... UNION SELECT ... ) AS t;
Parentheses around queries are subqueries -- think of this almost like a subroutine, where the inner query is executed first, then its result is evaluated in the context of the outer query. In your second example, you are defining a subquery where no outer query exists, hence a syntax error.
You might be able to transform the second example into valid sql simply by putting a SELECT before the first parenthesis and 'FROM DUMMY' after the closing parenthesis; try it.
I'm using phpmyadmin to test out some MySQL queries. I'm trying to write a larger, nested query, which is failing due to an unrecognized table alias, so I'm trying to debug smaller parts of it. However, I'm getting confusing errors when I try to use table aliases sometimes.
Can you explain why some of these queries throw errors?
SELECT * FROM table1 AS tablealias1 (works)
SELECT * FROM table1 GROUP BY userid (works)
SELECT * FROM table1 GROUP BY userid AS tablealias1 (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 'AS tablealias1
LIMIT 0, 25' at line 1 )
SELECT * FROM table1 WHERE userid=1 (works)
SELECT * FROM table1 WHERE userid=1 AS tablealias1 (same error as above)
(SELECT * FROM table1 WHERE userid=1) AS tablealias1 (same error as above)
You alias things to:
rename the column display's name
give it a reference name for later use elsewhere in the query statement (whether you use it explicitly or implicitly--as long as it could be used elsehere)
If you're not doing either, an alias makes no sense. You can't alias a result set unless it's used inside a subquery, then you need an alias to reference it.
This will work:
Select * FROM (SELECT * FROM table1 WHERE userid=1) AS tablealias1
as it implies
Select tablealias1.* FROM (SELECT * FROM table1 WHERE userid=1) AS tablealias1
Alone, this is garbage:
(SELECT * FROM table1 WHERE userid=1) AS tablealias1
This SQL works ok:
SELECT * from table_name where id IN (473,473,475);
This doesn't:
SELECT * from table_name where id IN CONCAT('(', '473,473,475', ')');
It says:
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 'CONCAT('(', '473,473,475', ')')' at line 1
Why?
I want to use it something like this:
SELECT * from table_name where id IN CONCAT('(', ids, ')');
ids is varchar and contains something like this:
473,473,475
Use FIND_IN_SET:
SELECT *
FROM table_name
WHERE FIND_IN_SET(id, ids);
The parameter to IN must be either a literal list or a subquery. CONCAT() returns a string, not a list -- SQL doesn't re-parse the result.
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).