Issues with the mysql query - mysql

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?

Related

Jow to use str_to_date

I need to use str_to_date function in MySql and return the correct date. Below is my try and and I am getting syntax error.
SELECT STR_TO_DATE('011122143859',%d%c%Y%l%i%S);
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 '%d%c%Y%l%i%S)' at line 1.
I need to return the timestamp value as 2022-11-01 14:38:59
Can someone show me what would be the correct syntax. I followed this resource.
https://database.guide/str_to_date-examples-mysql/
Update:
I found the correct syntax as below.
STR_TO_DATE('011122143859','%d%m%y%H%i%S')
SELECT STR_TO_DATE("2017,8,14 10,40,10", "%Y,%m,%d %h,%i,%s");
OR
SELECT STR_TO_DATE("August,5,2017", "%M %e %Y");

JSON_VALUE() with CONCAT() get syntax error

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

STRING_SPLIT in MySql- how to do it?

SELECT er.pNumber, er.name, ep.fPosition, eo.res
FROM events_shot er, events_shot_final ep, events_shot_final_res eo, events_gear era
WHERE era.idShot=er.idShot AND ep.idPhoto=era.idPhoto AND eo.idShot=era.idShot
AND era.idShot=42 AND eo.shotType='PRT'
AND er.pNumber IN (
SELECT *
FROM STRING_SPLIT(eo.photosId,'-')
)
shotsId is a String like 12-1-8-7... with n pNumber id separated by '-'
Unfortunately the query return this error:
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 '(eo.photosId,'-') )' at line 7
I can't change the database, how can I change my query?
Any help you can provide would be appreciated.
Fix your data model! Don't store multiple values in a single column.
Learn proper SQL syntax! Never use commas in the FROM clause. Always use proper, explicit, standard JOIN syntax.
With that said, you don't need to split the string. You can use string operations:
AND CONCAT('-', eo.photosId, '-') LIKE CONCAT('%-', er.pNumber, '-%')
But you should really start work on fixing the data model as soon as you get this query to work.

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

Request Update phpMyadmin

I want Update an SQL tablein phpMyAdmin, to change a values of a column.
My request is:
Update article set Id_LRU where ID_Article='DIEPRESTATION'
It return an error:
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 'where ID_Article='DIEPRESTATION'' at line 1
I changed it by using like because the type of the field is a Varchar:
Update article set Id_LRU where ID_Article like 'DIEPRESTATION'
Also it got the same error.
How can I correct it please .
You have syntax error.
Update article set Id_LRU ='YOURVALUE' where ID_Article like 'DIEPRESTATION'
Try above query.