JSON_VALUE() with CONCAT() get syntax error - mysql

Tested in mysql Ver 8.0.26
SET #my_json = '{"key1":"val1","key2":"val2"}';
SET #my_key = 'key1';
Everything's okay with the following attempt:
SELECT JSON_VALUE(#my_json,'$.key1');
JSON_VALUE(#my_json,'$.key1')
val1
Though with the following one I get an error:
SELECT JSON_VALUE(#my_json,CONCAT('$.',#my_key));
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 'CONCAT('$.',#my_key))' at line 1
Also if I try these other two attempts:
SELECT JSON_VALUE(#my_json,CONCAT('$.','key1'));
SELECT JSON_VALUE(#my_json,CONCAT('$.','"key1"'));
I am doing something wrong?
I need to use dynamic key names as #variable.

As JSON_VALUE() function description claims
path is a JSON path pointing to a location in the document. This must be a string literal value.
I.e. you cannot use an expression as path parameter.
Solution: use JSON_EXTRACT() function (accompanied with JSON_UNQUOTE() for string-type data). https://dbfiddle.uk/UdJGwopG

Related

check the manual that corresponds to your MariaDB server version for the right syntax to use near '>video_id=89'

I am using Laravel 5.5 and I am unable to delete the notification using the below query. It gives the following error :
SQLSTATE[42000]: Syntax error or access violation: 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 '>video_id=89'
at line 1 (SQL: delete from notifications where data->video_id=89).
DB::table('notifications')->WhereRaw("data->video_id=$video_id")->delete();
Why are you using data->video_id
Just try with
WhereRaw("video_id=$video_id")
The -> operator is shorthand for the JSON_EXTRACT() function in MySQL. Unfortunately, MariaDB does not support this shorthand syntax.
MariaDB did introduce the JSON_* functions in 10.2.3, but I do not know if they will work on columns in a query. Best thing you can do is try. You will need to rewrite your query to explicitly use the JSON_EXTRACT() function:
DB::table('notifications')
->whereRaw('JSON_EXTRACT(data, "$.video_id") = ?', [$video_id])
->delete();
If this doesn't work, then you'll either need to switch from MariaDB to MySQL, or you'll need to rewrite your query as a simple string search (using the LIKE operator).

MySQL + PHP In MySQL IF statement is wrong? How to create simple If statement MySQL 5.50?

I use this code but I see syntax error.
MYSQL 5.5.50.0
IF 2>1 THEN
SELECT "HELLO WORLD!"
END IF;
EDIT: This code Will not work. Because IF Statement only using FUNCTION and procedure.
Error is:
SQL-ERROR: SQLSTATE[42000]: Syntax error or access violation: 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 'IF 2>1 THEN
my readSql($sql) function is working most case. I think my MySQL code is wrong.
What is wrong?
Thank you.
EDIT:
IF Statement only using FUNCTION and procedure.
MySQL IF() takes three expressions and if the first expression is true, not zero and not NULL, it returns the second expression. Otherwise, it returns the third expression.
Depending on the context in which it is used, it returns either numeric or string value.
IF(expression ,expr_true, expr_false);
For example, you could have a query:
SELECT IF(1>3,'true','false');
#return false
If you just want return the string 'HELLO WORLD' , you could use:
SELECT CASE WHEN 2 > 1 THEN 'HELLO WORLD' END

Issues with the mysql query

i am trying to get the result from thquery, but i am getting error on the line 2, where i am using the cast function:
SELECT ticketstickets.`ID`, ticketsusers.`fname`, ticketsusers.`lname`, ticketstickets.`subject`,
ticketstickets.`created`, CAST(ticketstickets.modified AS VARCHAR(100)) as modified,
ticketstickets.`priority`, ticketstickets.`status`,
ticketsdepartments.`name` FROM ticketsusers, ticketstickets
LEFT JOIN ticketsdepartments ON ticketstickets.`DEPARTMENT_ID`= ticketsdepartments.`ID`
WHERE ticketstickets.parent = 0
AND ticketstickets.by=ticketsusers.ID
I tried with convert function too, but same error: [Err] 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
'VARCHAR(100)) as modified,
ticketstickets.priority, ticketstickets.`s' at line 2
Please see in the manual that a value cannot be casted to varchar(N). Instead you should cast to char(N) (so without "var").
use that instead
CAST(ticketstickets.modified AS CHAR(100))
Probably the issue is in your CAST statement.
Refer this post to correct your query:
How to use the CAST function correctly in a MySql SELECT statement?

Warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL

Warning: 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 'color:red'>
and this my code :
$db->query("UPDATE members set id='{$this->test['id']}',
lvl='{$this->userlvl}', ip='{$this->test['IP']}',
time='{$this->test['time']}',
linechat='{$this->test['msg']}'
WHERE user='{$this->test['name']}'");
I'm a beginner so please tell me what is must be ^^
I have tried this
$fixchat = mysql_real_escape_string($this->test['msg']);
$fixname = mysql_real_escape_string($this->test['name']);
$db->query("UPDATE members set id='{$this->test['id']}',
lvl='{$this->userlvl}', ip='{$this->test['IP']}',
time='{$this->test['time']}', linechat='{$fixchat}'
WHERE user='{$fixname}'");
but I got this error :
Warning: 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 ''\wellington\'' at line 1...
One or more of the values in your $this->test array has double quote characters in it. Every dynamic element to a query string must be escaped by passing it through the appropriate escape function (in your case mysql_real_escape_string()). That will escape the quotes so the string is interpreted correctly.
Side note: You should be using the mysqli PHP library instead of mysql, which is deprecated. Also, a better alternative solution is to use parameterized queries.
solved by : addslashes function
$fixchat = addslashes($this->test['msg']);
$fixname = addslashes($this->test['name']);
$db->query("UPDATE members set id='{$this->test['id']}',
lvl='{$this->userlvl}', ip='{$this->test['IP']}',
time='{$this->test['time']}', linechat='{$fixchat}'
WHERE user='{$fixname}'");

Mysql search and replace when involving "http://"

I'm trying to search and replace in MYSQL but get an error. I'm quessing it's because of the "http://"
Anyone got any suggestions when trying replace this type of thing?
Code entered:
update movies_news set select_page = replace(select_page, ‘http://movie’, ‘http://www.movie’);
But it throws 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 '://movie’, ‘http://www.movie’)' at line 1
Posting so it can be accepted:
update movies_news set select_page = replace(select_page, ‘http://movie’, ‘http://www.movie’);
contains smart quotes, which are not interpreted as normal single quotes, thus the syntax error. It should instead be
update movies_news set select_page = replace(select_page, 'http://movie', 'http://www.movie');
In general, be really careful about copying code to and from 'smart' text editors (Microsoft Word, etc)