Why MySQL error using table alias on a simple select statement? - mysql

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

Related

MySQL: Ordering of columns when using a wildcard with group by has odd behavior

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.

mysql query syntax error,LIMIT can't used before UNION any more after mysql8.0?

SQL:
select * from t_google_receipt limit 1 union select * from t_old_google_receipt limit 1;
the sql in my code excuted sucessful in my old mysql(Server version: 5.6.17-log MySQL Community Server (GPL)),but after updated to mysqlGroup(Server version: 8.0.11 MySQL Community Server - GPL), it excuted failed,
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 select * from t_old_google_receipt limit 1'
at line 1;
It actually changed in MySQL 5.7:
To apply ORDER BY or LIMIT to an individual SELECT, place the clause
inside the parentheses that enclose the SELECT:
(SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);
Note
Previous versions of MySQL may permit such statements without
parentheses. In MySQL 5.7, the requirement for parentheses is
enforced.
Beware anyway that if you don't provide individual ORDER BY clauses you'll get arbitrary rows (not even random).
Use parentheses:
(select * from t_google_receipt limit 1)
union
(select * from t_old_google_receipt limit 1);
Notes:
You probably want union all and not union.
The use of limit without order by is suspicious.
This assumes that the columns in the two tables are the same, defined in the same order, and have compatible types.
For union and union all you have to consider below point
Each SELECT statement within UNION must have the same number of
columns
The columns must also have similar data types
The columns in each SELECT statement must also be in the same order
So in case of two tables union it is bad practice to use select all(select *) column from table rather it is preferable and less error to use column name explicitly like below
select col1,col2... coln from t1
union
select col1,col2.....coln from t2

Error #1064, with Select column,* from table

Okay, just was debugging something and I found a wired error, probably it should be documented somewhere, but I search MYSQL documentation and didn't found anything. Here is the sql query, that produce 1064 error near * from table,
Select char_length(zip), zip, * from tbllocations
But this below one works fine:
Select *, char_length(zip), zip from tbllocations
Cannot we use * at end of column list? I test this query on MySQL 5.5.41 and MySql 5.0.95. I didn't notice this error before. I rewrite query to avoid special/hidden characters.
Aren't they same query except change in order of columns? Any explanation or resource to look for one.
EDIT:
Just run Select char_length(zip), zip, tbllocations.* from tbllocations and it works fine? So looks like I am hitting some bug? or anything logical, I am missing?
From MySQL docs:
A select list consisting only of a single unqualified * can be used as shorthand to select all columns from all tables:
SELECT * FROM t1 INNER JOIN t2 ... tbl_name.* can be used as a qualified shorthand to select all columns from the named table:
SELECT t1.*, t2.* FROM t1 INNER JOIN t2 ... 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
SELECT AVG(score), t1.* FROM t1 ...

MySQL syntax error near "from (subquery) 'table'"

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;

similar functionality to SQL Server's EXCEPT operand in MySQL?

Is there an operand/function/command in MySQL similar to the EXCEPT operand in SQL Server?
EXCEPT returns any distinct values from the left query that are not also found on the right query.
This statement should give me the distinct values.
SELECT * FROM table1
EXCEPT
SELECT * FROM table2;
How can this be achieved in MySQL?
The best you could do is use a NOT EXISTS. Something like:
SELECT DISTINCT *
FROM table1
WHERE NOT EXISTS(SELECT NULL
FROM table2
WHERE table1.x = table2.x)