MySQL inline (SELECT ... WHERE) not working on 5.6 - mysql

I'd like to run a query like the following:
SELECT GROUP_CONCAT((SELECT columnA WHERE columnB)) AS filtered_list FROM ...
This works on MySQL 5.7 but not 5.6. I'm wondering if there's a workaround or if this is simply unsupported before 5.7. I can't find anything in the update documentation that would explain the change in syntax.
Here's a simpler version for testing:
SELECT 1 AS a, (SELECT 1) AS b, (SELECT 1 WHERE 1) AS c
This works fine on MySQL v5.7, but on v5.6 the last column will throw a syntax error. Does anyone know a way to make this work?

Related

Cannot use regexp_substr in the old version MySQL Query Browser

I am using MYSQL Query Browser version 1.2.11, and write the below SQL query cannot work in MYSQL Query Browser version 1.2.11.
select id,name
from(select t.*,cast(regexp_substr(name,'[0-9]+') as unsigned) col1
,cast(regexp_substr(name,'[0-9]+',1,2) as unsigned) col2
,cast(regexp_substr(name,'[0-9]+',1,3) as unsigned) col3
,cast(regexp_substr(name,'[0-9]+',1,4) as unsigned) col4
from filing_code_management t) c
order by col1,col2,col3,col4
I have tried this SQL query to use in the online dbfiddle, just SQL 8.0 can work in https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=e43d94d0418854375a07c2bbf1e44339
May I know got any method can replace this SQL query code to suitable in my old MYSQL DB version 5.5? My side need to use old version to run the SQL. Hope someone can guide me, thanks.
The error appears FUNCTION db_12668458.regexp_substr does not exist in This old version of DB.

Warning "A non-numeric value encountered" when using parentheses with LIMIT statement in phpmyadmin

I am getting a PHP warning when running a query in phpMyAdmin that includes both parentheses and a LIMIT statement. For example:
(SELECT * FROM tableA LIMIT 1)
The warning is:
A non-numeric value encountered
I am using PHP version 7.2 and MySQL 5.6.42 and phpMyAdmin version 4.9.0.1
What might cause this warning?
EDIT:
My issue is not a duplicate of this, because no matter which column or table I use, I get the same warning
EDIT2:
Because people asked for an actual UNION query:
(SELECT name FROM tableA LIMIT 1)
UNION
(SELECT name FROM tableB LIMIT 1)
This was the scenario I encountered the warning.

"group by desc" syntax error on mysql 8.0 which is fine on 5.7

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);

Window function in MySQL queries

Is there a way to use window functions in MySQL queries dynamically within a SELECT query itself? (I know for a fact that it is possible in PostgreSQL).
For example, here is the equivalent query in PostgreSQL:
SELECT c_server_ip, c_client_ip, sum(a_num_bytes_sent) OVER
(PARTITION BY c_server_ip) FROM network_table;
However, what would be the corresponding query in MySQL?
Starting MySQL 8.0, you can now use OVER and partition, so consider upgrading to the latest version :)
Hope this might work:
select A.c_server_ip, A.c_client_ip, B.mySum
from network_table A, (
select c_server_ip, sum(a_num_bytes_sent) as mySum
from network_table group by c_server_ip
) as B
where A.c_server_ip=B.c_server_ip;

Can not Access Aliased Columns in Select Statement (MySQL)

I have a problem with Aliased Columns in MySQL!
My Query:
SELECT Price AS Pr, (Pr*10/100) FROM MyTable;
MySQL WorkBench Error: UnKnown Column 'Pr' in Field List !!!
I tested my query in W3Schools with no error !
I tested my query in W3Schools with no error!
This doesn't prove that your query is valid.
You can only use aliases in GROUP BY, ORDER BY or HAVING clauses. Your usage variant is not allowed, because the value of alias is not known when MySQL is selecting the 2-nd column.
I've got a suspicion that W3Schools uses MS Access to run user queries, and MS Access does allow such atrocity as referencing column aliases in a SELECT clause that are defined in the same SELECT clause.
The standard doesn't allow this and MySQL does follow standard in this particular case.
As for solution to your problem, I can see two options.
The more generic solution, which would run in probably any SQL product, would be to use a derived table:
SELECT
Pr,
(Pr * 10 / 100) AS SomethingElse
FROM
(
SELECT
SomeComplexExpression AS Pr
FROM MyTable
) AS sub
;
The other option would be to use a variable, which is MySQL-specific:
SELECT
#Pr := SomeComplexExpression AS Pr,
(#Pr * 10 / 100) AS SomethingElse
FROM MyTable
;
Finally, if you need to test/demonstrate if something can/cannot work in MySQL, I'd recommend using SQL Fiddle.