Query SELECT COALESCE(SELECT, ...) not working with MySQL 8.0 - mysql

With MySQL 5 this query worked without any issue:
SELECT 1+COUNT(*) AS rank
FROM userpoints
WHERE points > COALESCE((SELECT points FROM userpoints WHERE userid=(SELECT userid FROM users WHERE handle='abc123' LIMIT 1)), 0)
With MySQL 8.0.23 it throws an error:
MySQL query error 1064: You have an error in your SQL syntax
Is it not allowed to "embed" SELECTs into other SELECTs anymore? See the userid=(SELECT... part.
Or is it a problem with COALESCE?

Related

Delete Query in MySQL 5.7 not working because error SQL Error [1093] shown

I tried to see other similar problems and i've done the same as other people but my delete clause is not working.
DELETE gl FROM int_id2_game_server.game_link gl
WHERE gl.game_pack_id = 794 AND gl.id NOT IN (
SELECT * FROM (SELECT parent_id FROM int_id2_game_server.game_link
WHERE gl2.game_pack_id = 794 AND gl2.parent_id IS NOT NULL)gltemp);
Error shown as SQL Error [1093] [HY000]: You can't specify target table 'gl' for update in FROM clause
Which modification I have to do?

Same query, different results

I'm running a cron daily, and keep records saved in a table. Wrote a query to see the daily progress of the users, but facing an issue.
I have local and production environments with separate data bases.
SELECT *, LAG(value) OVER(PARTITION BY node_id ORDER BY created_at) old_value
FROM ledger WHERE ledger_type_id = 1
This is the part of the query that is failing. I think the db driver has something to do it. The query was working on the prod data base, from my SQL client, but I just noticed that it is running MySQL 8, and the prod db is 10.3.16-MariaDB.
Tried the same query on my local data base - MySQL 5.7, and got this error:
[2019-08-06 16:12:47] [42000][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 '(PARTITION BY node_id ORDER BY created_at) AS old_value
After this I did 2 more tests:
Ran the query on the prod server it self, via phpmyadmin, and the server complete died, for a few minutes.
Ran the query from my code on the prod environment (laravel 5.8):
DB::select(DB::raw('
SELECT *, LAG(value) OVER(
PARTITION BY node_id ORDER BY created_at
) old_value
FROM ledger WHERE ledger_type_id = 1
'))
and got this in my laravel log:
production.ERROR: SQLSTATE[42000]: Syntax error or access violation:
1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is
illegal if there is no GROUP BY clause
Am I doing something incorrect?

#1064 - You have an error in your SQL syntax - not exist - xampp

I have made a sql statement that works normal in a shared server. but since I migrated the database to a local one
it showing a #1064 - You have an error in your SQL syntax
INSERT INTO shops (shop_name, googleid, address, city, country, open_now, logogroup, idshopgroup, shop_group, image, website)
select '$shopname', '".$key["googleid"]."','$address','".$key["city"]."',
'".$key["country"]."', '".$key["open_now"]."','".$key["logo"]."','".$key["groupid"]."','$groupname','".$key["photo_reference"]."', '".$key["website"]."'
WHERE NOT EXISTS (
SELECT googleid FROM shops WHERE googleid = '".$key["googleid"]."'
) LIMIT 1;
I fixed the problem by adding a "from 'tablename'" before the "WHERE" clause

MySQL phpmyadmin - SELECT FOR UPDATE not recognised

I'm writing a PHP program and wanna to implement row-level locking to avoid concurrent user update/delete for the same record.
But I hit error "Unrecognised keyword" when using SELECT FOR UPDATE. Table type is innoDB.
Am i missing any setup for my database?
SELECT * FROM companyTable
WHERE companyId = "0000001"
FOR UPDATE;
Error
Static analysis:
1 errors were found during analysis.
Unrecognized keyword. (near "FOR" at position 57)
SQL query: Documentation
SELECT * FROM companyTable WHERE companyId = "0000001" FOR LIMIT 0, 30
MySQL said: Documentation
#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 'LIMIT 0, 30' at line 3
There Is an Syntax Error Try this:
Its Select All Records From 1 - 30
SELECT * FROM companyTable WHERE companyId = "0000001" ORDER BY id LIMIT 30;
First problem seems to be, that in your SQL query is missing keyword UPDATE
SELECT * FROM companyTable WHERE companyId = "0000001" FOR LIMIT 0, 30
And the second issue can be, that syntax SELECT ... FOR UPDATE in MySQL doesn't support LIMIT.
So your SQL query should be:
SELECT * FROM companyTable WHERE companyId = "0000001" FOR UPDATE
In phpmyadmin, it is difficult to remove auto added LIMIT clause - try another MySQL client.
PhpMyAdmin is not build to handle(Test) transaction control, use Mysql Console or php session instead
and use "begin" to start the Transaction before the code
begin;
select * from `tblx` where `idx`=1 for update;
update `tblx` set `field`='xx' where `idx`=1;
commit;

SQL Query in XAMPP

SELECT * FROM customers WHERE Age > ALL (SELECT Age FROM customers WHERE Salary > 6800);
Actually I am trying to learn the usage of ALL in SQL. But unfortunately when I am trying to execute this query it's giving me the error in XAMPP.
Static analysis:
2 errors were found during analysis.
Unrecognized keyword. (near "ALL" at position 36) Unexpected token.
(near "(" at position 40) SQL query: Documentation
SELECT * FROM customers WHERE Age > ALL ( LIMIT 0, 25
MySQL said: Documentation
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 'LIMIT 0, 25' at line 1
I would like to know what is the exact syntactical error in my query.
Here's my Table:
Customer Table
There is nothing syntactically wrong with your query, you have an unfortunate version of phpMyAdmin which messes it up. The problem was present in 4.5.1, not present in 4.6.4, it was fixed some time in between.
This is the query that you send (OK):
SELECT * FROM customers WHERE Age > ALL (SELECT Age FROM customers WHERE Salary > 6800);
This is the query that server receives from phpMyAdmin (very wrong):
SELECT * FROM customers WHERE Age > ALL ( LIMIT 0, 25
Hence the problem.