MySQL Workbench "->" Support - mysql

It looks like MySQL Workbench is now supporting JSON functionality, however I'm still seeing parser errors on MySQL 5.7.9 functionality, such as the "->" operator.
When I use the following query, I'm getting a syntax error over the "$.test" portion:
Record
record: {"test": 123}
Query
SELECT test->"$.test" FROM table
The query still executes successfully, however I'm curious as to why the syntax parser is incorrectly showing an error.

OK, the problem is probably something else than what I posted in my comments. You are using double quotes, which represent strings only if the ANSI quotes are not enabled (then they wrap identifiers). Use single quotes instead.

Related

MariaDB search using REGEXP

I am having trouble formulating a valid regular expression for searching in mysql, on a MariaDB database.
I am doing these searches in phpMyAdmin, but will eventually also being doing them in the Search-Replace-DB tool (which is also MySQL based).
Let's say the table I am searching is table-x, and the field is field-y. I need to find the string \root\localhost/cf/.
Here's an example of the data that contains this string:
O:8:"stdClass":2:{s:4:"file";s:58:"\root\localhost/cf//wp-content/wflogs/config-transient.php";s:4:"hash";s:64:"96e5846450f943eeb94993173c13f1dc28a824eff513d1d8019011515b8729f9";}
I tried this regular expression, which works in my expression tester, but doesn't work in MySQL:
\\root\\localhost/cf/. The entire query being,
SELECT * FROM table-x WHERE field-y REGEXP '\\root\\localhost/cf/'.
This gave me the error, #1139 - Got error 'PCRE does not support \L, \l, \N{name}, \U, or \u at offset 6' from regexp.
To get around the \l in my expression, I tried the following:
SELECT * FROM table-x WHERE field-y REGEXP '\\root\\.ocalhost\/cf\/'
This works in my reg exp tester. But it gives no results in phpmyadmin. It did get rid of the PCRE error message though.
What's the correct way to go about this?

Difference in behavior on CONCAT_WS between systems

So, this is a simple situation but I wanted to understand what's causing this issue. I have the following code (modified for example):
SELECT `Transactions`.*, CONCAT_WS(" ", `People`.`first_name`, `People`.`last_name`) AS full_name ...
On my local machine I have:
Windows 10
Apache 2.4.25
PHP 7.4.11
MySQL 5.7.25
With that combination the following code works fine.
On the remote server I have:
Ubuntu 20.04.1 LTS
Apache 2.4.41
PHP 7.4.3
MySQL 8.0.19
So, I have a section which uses a data table and the data table uses server-side processing to obtain the information. On my local it shows the information correctly, but on my remote server I always got an empty array. So I tried executing the same SQL command in my remote server and I got this error:
#1054 - Unknown column ' ' in 'field list'
My SQL was correctly formed so I thought maybe the problem was related to the CONCAT_WS function.
So I decided to modify it to:
SELECT `Transactions`.*, CONCAT_WS(' ', `People`.`first_name`, `People`.`last_name`) AS full_name ...
I basically changed CONCAT_WS(" ", to CONCAT_WS(' ', and the code worked as intended.
I am not sure if this affects in some way, but is this a MySQL change in requirements for the usage of CONCAT_WS or something else?
Is it ok if I use it with single quotes elsewhere?
I suggest you run this on both systems:
SELECT ##sql_mode;
You will find that on your 8.0 server, the sql mode includes either the modes ANSI or ANSI_QUOTES.
Explanation:
The double-quotes have different meanings in MySQL depending on which sql mode is in effect.
By default, double-quotes are the same as single-quotes: they delimit a string literal.
If the sql mode is ANSI or ANSI_QUOTES, then the double-quotes delimit an identifier, acting like the back-ticks.
So the same code can behave differently on different MySQL instances. This has nothing to do with the difference between 5.7 and 8.0, because the sql mode behaves the same on these two versions. Neither version enables the ANSI or ANSI_QUOTES modes by default, so you or someone else must have enabled that mode on your 8.0 server.
This is why in this expression:
CONCAT_WS(" ", ...)
The first argument " " is treated as a string literal on one server, and on the other server it is treated as a column whose name is (which is legal SQL, even if it's weird).
It's safest to always use single-quotes to delimit a string literal, and to always use back-ticks to delimit an identifier.
Never use double-quotes for either case in MySQL, because your code that uses double-quotes in SQL queries will break if someone changes the sql mode.

Escaping Reserved Characters In A WHERE ... MATCH ... AGAINST... Statement?

I have a query that contains some characters that cause a syntax error because contains reserved characters and I am battling to understand how to escape the string correctly.
The query is:
SELECT * FROM `products`
WHERE MATCH (code, description)
AGAINST (UPPER(+("intel"*) +("cpu"*)) IN BOOLEAN MODE)
But when I run this query I get the following 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 ') +("cpu"*)) ) IN BOOLEAN MODE)' at line 1
Okay so fine it does not like the ) as it would indicate that the AGAINST is being closed however it is not (yet). So I tried to escape it with a backslash but it still throws the same error.
If I try this in PHP using a prepared statement while binding the search string +("intel"*) +("cpu"*) into the statement it works. So it seems that the way that it escapes it is not with a backslash or that there is something else.
So I was looking at the PHP documentation for mysqlescapestring and I saw that it: "prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.".
Which indicate that the single and double quotes need to be escaped and I tried to do this but it just throws the same syntax error but on the double quote character, i.e. to use near '\"intel\"*\)...
I do understand that it would be best to use prepared statements and that this solves the problem but I just want to understand what I have done wrong here and how I could escape a string like this within an AGAINST clause as I have done here.
If anyone could suggest where I have going wrong with this then it would be greatly appreciated. Thank you.
Well, I managed to solve this.
I read an answer on another so question that helped me here. What I realised is that when a prepared statement includes the relative object it encapsulates it with quotes, so in actual fact UPPER(?) would become UPPER("prepared string") which means that UPPER(+("intel"*) +("cpu"*)) should actually be UPPER('+("intel"*) +("cpu"*)').
So the result is:
SELECT * FROM `product`
WHERE MATCH (code, description)
AGAINST (UPPER('+("intel"*) +("cpu"*)') IN BOOLEAN MODE)
Which does work without syntax errors.
As a note, if you are escaping strings in MySQL it would be worthwhile to note that MySQL uses C escape syntax in strings.

Escaping special characters in phpMyAdmin query

I am trying to replace the string 1"/850 in the description field of a Magento database with 1-inch/850. I have come up with some variations of this
update catalog_product_entity_text set value = replace(value, ‘1"/850’, ‘1-inch/850’);
I have tried breaking with something like 1\"/850 or other variations and I keep getting an error message. Any suggestions as to the right syntax to use in this case?
It looks like you are using curly quotes. MySQL only understands the straight ones, so write '1"/850' and '1-inch/850'.
If this is not the issue, what error message do you get?

Mysql handling with single quotes conflict

I'm using joomla to develop sites, but I'm having a strange error. I have a syntax error in the following code:
$q = "TRUNCATE TABLE ".$db->quote('#__csvi_available_fields');
Which give output on runtime:
TRUNCATE TABLE 'erx_csvi_available_fields'
But mysql shows an error:
JDatabaseMySQL::query: 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 ''#__csvi_available_fields'' at line 1
SQL=TRUNCATE TABLE '#__csvi_available_fields'
The strange thing is when I run without quotes, it runs normal:
TRUNCATE TABLE erx_csvi_available_fields <-- works without problem
Any idea what went wrong here ?
As other have said the wrong quotes have been added.
When using Joomla's JDatabase to provide quoting there are two different functions you can call one for values and another for database, table or column/field names.
To make your example line work you need to use quoteName() as follows:
$q = "TRUNCATE TABLE ".$db->quoteName('#__csvi_available_fields');
The $db->quote() is used to quote values being used in the SQL.
You can read through /libraries/joomla/database/database.php for an idea of how the abstraction is supposed to work.
don't use single quotes "'". use "`" (left to the numbers on your keyboard). normal single quotes are for strings, same as double quotes
Single quotes are used for strings, you should use backticks for names.
From the MySQL manual:
The identifier quote character is the backtick (`)
Also have a look at this Stackoverflow question: Using backquote/backticks for mysql queries