$exams=[{"id":1}];
$toReturn['exams'] =exams_list::whereIn('examClasses',$exams)->get()->toArray();
How to do this?
You can do as below
$exam_id = 1;
$toReturn['exams'] =exams_list::where('examClasses->"$.id"',$exam_id)->get()->toArray();
//As per your question
//$exams=[{"id":1}]; =======> Wrong way
$exams=["id" => 1];
$toReturn['exams'] =exams_list::where('examClasses->"$.id"',$exam["id"])->get()->toArray();
See example given below, Refer https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html
column->path
In MySQL 5.7.9 and later, the -> operator serves as an alias for the JSON_EXTRACT() function when used with two arguments, a column identifier on the left and a JSON path on the right that is evaluated against the JSON document (the column value). You can use such expressions in place of column identifiers wherever they occur in SQL statements.
Related
I have some columns VARCHAR which contains JSON content with relation to other rows of the same table, and i need to convert this into comma separated keys to use a WHERE IN.
Example column content:
[{"manuel":"Manuel Fernandez"},{"marta":"Marta Flores"}]
And what i need is:
manuel,marta
This is part of a bigger query where this conversion should be applied to more than one column and thats why i think the best solution is doing this using Mysql REGEX
Update 1
Mysql version 5.7.25
You need to remove [, ], {", and everything from ":" to the next } from the string.
regexp_replace(columnName, '\\[|\\]|\\{"|":"[^}]*\\}', '')
You need MySQL 8.0 to get a built-in REGEXP_REPLACE() function. If you're using an older version, see How to do a regular expression replace in MySQL?
I have a table in MySQL where each row contains JSON returned from another system. The JSON will look something like:
[{"userId": "Dave"},{"userId": "Mary", "errorCode" : "DB Fail"}, {"userId": "Lorenza", "errorCode": "Web Error"}]
and I'm only interested in the members of the array containing an error code. In the future, these will be parsed into seperate rows of their own table, but in the meantime does MySql offer a way to extract only these with an errorCode?
I can use JSON_EXTRACT to extract the errorCodes only
JSON_EXTRACT(jsonData, '$[*].errorCode') AS errorCodes
but I really want the rest of the member (userId in the example above)
You could use the JSON_CONTAINS function to find the records with errorCode and then then use JSON_EXTRACT on those records. Put the JSON_CONTAINS in the where clause
I don't think you could do this with a single query without known boundaries of the number of elements, but you could use a stored procedure to run a loop.
e.g. each iteration runs LOCATE to find the position of "errorCode", and uses that location to run SUBSTR and/or SUBSTRING_INDEX to get the userid value and append it to another variable. The looped variable would just be the offset used in the LOCATE query.
I am trying to create a stored procedure, which receives a string and places it in the WHERE clause after processing it.
I created the function and when I call it from the body of a SQL statement, it returns the correct values:
('J1245',j3456','j1098')
However, when I call the function inside the WHERE clause as shown below, SQL does not show any records.
(Altcode in dbo.myfunction(#codes))
When I hard code it inside the WHERE clause, the SQL statement shows records.
(Altcode IN ('J1245',j3456','j1098'))
I suspect the function is returning a string (VARCHAR). If we reference the function anywhere in a SQL statement, the value returned by the function serves as a scalar value. The contents of the value don't change that. Any punctuation, single quotes, backticks, parens, commas, keywords, identifiers, et al. that happen to appear in the string are just part of the value.
The parser sees the return from the function simply as a single value, not as part of the SQL text.
It's not possible (in a single statement) to make the value returned by a function "become" part of the SQL text to be parsed.
As an example, in the WHERE clause, if we write:
WHERE altcode IN ( myfunc() )
The function myfunc is going to be evaluated, and the return will be single value, of a certain datatype, for example maybe a VARCHAR. Any parens or commas within the value are not interpreted as part of the SQL text. Those are just characters that are within the value.
It's as if we wrote SQL like this:
WHERE altcode IN ( ? )
And supplied a single value in place of the question mark placeholder. The SQL parser isn't seeing a list of values, it's not seeing any SQL to be executed. All the statement is seeing is a value.
It matters not one whit that we might supply a value that looks like SQL text, for example:
WHERE altcode IN ( '(SELECT code FROM all_altcodes)' )
That would be equivalent to writing
WHERE altcode = '(SELECT code FROM all_altcodes)'
And that is going to look for an exact match of altcode to the string literal.
Seems like there's a single quote missing in the value returned by the function, but maybe that's a typo in the question.
To get the string value returned by the function included as part of the SQL text, we would need to use dynamic SQL.
We would have to first call the function, and return the string. And then do some string concatenation to come up with another string that contains the SQL statement we want to execute, and then execute that string as a SQL statement.
So that would be two separate statement executions... one to get the function evaluated; and a second statement that is dynamically constructed, as a string, incorporating the value returned by the function.
(The question is tagged "MySQL", but I suspect this question is actually regarding SQL Server (given the reference to dbo. ?)
Preparing and executing dynamic SQL is similar in MySQL and SQL Server, but there are differences in the syntax.
Say for example my function accepts a parameter called 'entityType', and according to that it queries the table entityType_other_stuff.
Is this possible to implement in JOOQ on runtime?
The correct way to create dynamic org.jooq.Table objects by name is to use DSL.table(Name) as in:
String parameter = "entityType";
Table<?> table = table(name(parameter + "_other_stuff"));
If you use the standard Settings.renderNameStyle QUOTED, then the identifier will be quoted and escaped, and thus SQL-injection safe.
For more information, see the manual: https://www.jooq.org/doc/latest/manual/sql-building/names
what is equivalent's of R ('NA'|'NULL'|'None') value in RAPIDMINER? Or what is some other value for denoting that this value should not be taken into consideration while making calculations, or that it is missing?
Missing values show up as '?' in the data view of example sets and they are counted in the meta-data view. Imported data can obviously have them and you can generate your own using the Declare Missing Values operator or within the Generate Attributes operator using a code fragment containing an invalid mathematical operation like 0/0. You can test for the presence of a missing value by using the missing() function within the Generate Attributes operator.
It is probabbly the question mark: ?