What's wrong with this query? - sql

using MySQL version 4.0.27:
UPDATE `t` SET `col_x` =
(SELECT `col_x` FROM `t` WHERE `col_y`='123456') WHERE `col_y`= '456789'
Error message: #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 'SELECT t FROM b WHERE col_x='1234
I tried LIKE '%123456%'

I'm sorry to disappoint you, but subqueries are not supported in your version if MySQL.
Subqueries have been introduced in ver 4.1 according to MySQL Dev Zone

You can't select and update from the same table in a query.
reference: mysql update documentation

Check out this page. Apparently you need to set your SQL mode to 'ANSI QUOTES'

Related

SKIP LOCKED DATA Syntax issue in DB2

I am trying to execute simple select query with SKIP LOCKED DATA
but getting syntax error. Below is the sample query
SELECT ELEMENT FROM WORKQUEUE
WHERE PRIORITY = '1' AND STATUS='OPEN'
SKIP LOCKED DATA;
Got error as below
DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=SKIP;
<query_expression>;END-OF-STATEMENT, DRIVER=3.61.86
But as per documents it is valid query. Please let me know if I am doing something wrong?
I suspect you are not using Db2 for z/OS 10.0.0
I suspect you are using e.g Db2 11.1 and you need a manual page from that Db2 platform such as
"Evaluate uncommitted data through lock deferral" - https://www.ibm.com/support/knowledgecenter/SSEPGG_11.1.0/com.ibm.db2.luw.admin.perf.doc/doc/c0011218.html
because Db2 for Linux, Unix and Windows does not support the SKIP LOCKED DATA clause directly

SQL error while using limit keyword in Derby

I have used limit keyword as:
"select * from empl3 limit "+4
But I am getting the error as:
Syntax error: Encountered "4" at line 1, column 27.
I am using Derby database.
As documented in the manual there is no LIMIT clause in Derby.
Derby uses the SQL standard for limiting the number of rows:
select *
from empl3
fetch first 4 rows only;
I believe the +4 after the select statement is giving you the error. You should return all of your data necessary for you page select * from empl3, but handle the paging in your page itself.
The key is in your words I am using derby database - see Does Derby support a LIMIT command.
You can either use the workaround suggested in the linked FAQ, or implement paging on your own, as suggested by WEI_DBA.

MariaDB syntax error 1064, check the manual that corresponds to your MariaDB server version

I'm trying to manipulate an existing database found here https://github.com/SchadLucas/xbmcTweaks/blob/master/music.sql
But for some reason MariaDB doesn't like the syntax although it seems to be right according documentation
Error
SQL query: Documentation
DROP VIEW IF EXISTS artistview
CREATE VIEW artistview AS
SELECT DISTINCT
artist.idArtist AS idArtist,strArtist,strBorn,strFormed,artistinfo.strGenres,artistinfo.strMoods,artistinfo.strStyles,strInstruments,strBiography,strDied,strDisbanded,strYearsActive,artistinfo.strImage,strFanart
FROM artist
LEFT OUTER JOIN artistinfo ON artist.idArtist = artistinfo.idArtist
JOIN album ON album.strArtists = artist.strArtist
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 'CREATE VIEW artistview AS
SELECT DISTINCT
artist.idArtist AS idArtist,strArt' at line 2
Version running is:
Server: Localhost via UNIX socket
Server type: MariaDB
Server version: 5.5.37-MariaDB - Source distribution
Protocol version: 10
User: root#localhost
Server charset: UTF-8 Unicode (utf8)
I'm no SQL expert and can't seem to find what is going wrong.
Any ideas?

How do I use TOP instead of them LIMIT keyword in CakePHP with MSSQL?

CakePHP constructs all its queries ending with "LIMIT X", which is the MySQL syntax, not plain SQL and Microsoft SQL Server won't accept it. Even if you're using the ODBC driver it'll generate queries like:
SELECT "Content"."id" AS "Content_dot_id",
"Content"."name" AS "Content_dot_name"
FROM "contents" AS "Content" WHERE (1=1) LIMIT 1
Which gives:
37000: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'LIMIT'.
Because you want:
SELECT TOP 1 "Content"."id" AS "Content_dot_id",
"Content"."name" AS "Content_dot_name"
FROM "contents" AS "Content" WHERE (1=1)
The code that takes on the "LIMIT" is in dbo_source.php rather than one of the DB-specific files (I think it's function limit($limit, $offset = null)).
Is there some setting I can toggle to switch to the TOP syntax instead of LIMIT?
The resolution that I found from a Google cached version of an old CakePHP trouble ticket was "Use the MSSQL dbo".
So what you need to do is to configure CakePHP to use the MSSql Driver and it will start using the "TOP" syntax. Here is a blog post talking about how to do that: http://www.johnmckinzie.com/2007/06/29/sql-server-2005-and-cakephp/

MySQL Subquery is failing on MySQL 4.0 with invalid syntax error

I'm running a pretty basic subquery on MySQL 4.0.30.
My goal is to get the user permissions from the mysql.user table for any user with grants on a specific database, as noted in the mysql.db table. The query looks like this:
mysql> select * from mysql.user where User IN
(select User from mysql.db where Db='db_name')\G
As you can see, it's pretty basic and follows the subquery syntax in the MySQL manual. However, when I execute that, it errors with the following response:
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 'select
User from mysql.db where Db='db_name')' at line 1
I also tried the command with = ANY instead of IN. I've run the same query on 4.1 and 5.0 versions of MySQL. Any help or insight on this is appreciated. Thanks
Ok, so it turns out I just didn't check the manual closely enough:
Starting with MySQL 4.1, all subquery forms and operations that the SQL standard requires are supported, as well as a few features that are MySQL-specific.
http://dev.mysql.com/doc/refman/4.1/en/subqueries.html