I am using XAMPP for Linux 5.6.30 and MySQL version is 10.1.21-MariaDB
I have one table 'employee'
id data
1 [{"name":"abc","age":"56","dob":"2017-05-15","about":"test"}]
2 [{"name":"xyz","age":"26","dob":"2017-09-20","about":"test"}]
3 [{"name":"pqr","age":"96","dob":"2017-03-05","about":"test"}]
Table field data contains JSON string.
I want to find name from JSON and my query efforts is
SELECT * FROM `tbl_employee` WHERE JSON_EXTRACT(data,"$.name") = "abc"
SELECT * FROM `tbl_employee` WHERE data->"$.name" = "abc"
but it gives me an error
> #1305 - FUNCTION testing.json_extract does not exist
I take reference from this link
Please give me suggestions regarding this?
Is my database not able to do that OR I have used wrongly?
The function JSON_EXTRACT in maria db is available since version 10.2.3,
you can read the documentation here.
You have to update your maria db installation to use that function.
I think, the version you are using it is not exist for your case (JSON_EXTRACT). If you are work on php, you can use php native function json_decode().
Further can be found here : Error code 1305
Related
I was storing Json data in Php database like this ["4","2"]. Problem is that i want to get data from json id using where clause . Is there any solution for that?
I was also use this JSON_CONTAINS but not working.
SELECT membermaster.* FROM membermaster WHERE membermaster.status = 'Active' AND membermaster.role = 'client' AND JSON_CONTAINS(client_type,"2")
Sorry for my bad english. Thank You.
Can you tell which Mysql version you are using because JSON_CONTAINS only will work in greater than equal MySql 5.7 version? It also depends on the server setting and database open extension settings.
If you want to get data using the where clause you can write a query like below.
SELECT membermaster FROM membermaster WHERE membermaster.status = 'Active' AND membermaster.role = 'client' AND client_type like '%"2"%';
Or if you want you can use regex also.
I want to load data from a SQLite database in PHP using Atlas.Orm.
When I run the following snippet, I get a set of 1773 results, but each result is the same!
$atlas = Atlas::new('sqlite:[Path To Database]');
$result = $atlas->select(Stop::class)->fetchRecords();
Can anyone tell me whats wrong here?
After several hours of desperation, I found the issue by myself. The ORM needs the PRIMARY_KEY constant in the corresponding *Table class to be set, otherwise fetching records will fail like this.
I am storing some data inside mysql using the JSON Type field.
When I try to select query my json data field in Laravel 5.6, I get an exception
$transactions = DB::table('transactions')
->select('transactions.uuids_json', '... other columns')
....
SQLSTATE[HY000]: General error: 2036, query exception
If I remove the uuids_json field, everything works normal. Do I have to somehow use raw statements or something?
The column uuid_json data field contains rows like this:
["1a7b29b8-5009-4266-8192-508930f2f92a", "3d52cfd5-d3c0-467f-8da1-cf81c344ad20", "cbe6e7fb-d806-49e2-8616-3c28afa012fe", "dfda9df5-2cbf-4cb8-aa54-a6dc23f73995"]
Thank you for any help!
This sounds related to https://bugs.php.net/bug.php?id=70384
Make sure your PHP install is using the mysqlnd 5.0.11 or later, which according to that bug log should include the fix.
Check php -i or phpinfo() for the version of mysqlnd you use.
If you can't upgrade to a version of the library that supports the JSON data type, a workaround is to CAST the JSON column to a string:
->select(\DB::raw("CAST(transactions.uuids_json as CHAR) as uuids_json"), ...)
So, I want to query the notifications table in Laravel by comparing a certain ID with the data column. This is how data column looks like:
{
"Message": "some message",
"id": 3
}
Now, I need to select all the notifications that have an ID that is equal to 3. Here is how I was trying to do it:
DB::table('notifications')->where('data->id', '3')->get();
But this throws 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 '>'$."id"' =
?' at line 1 (SQL: select * from notifications where
data->'$."id"' = 3)
I am losing my mind here, can anyone help me out?
There's nothing wrong with your query. It's your environment.
Problem
Laravel's MySqlGrammar translates the field->key notation in field names (on Laravel side) into field->'$.key'-style extractions (on MySQL side):
/**
* Wrap the given JSON selector.
*
* #param string $value
* #return string
*/
protected function wrapJsonSelector($value)
{
$path = explode('->', $value);
$field = $this->wrapValue(array_shift($path));
$path = collect($path)->map(function ($part) {
return '"'.$part.'"';
})->implode('.');
// Here:
return sprintf('%s->\'$.%s\'', $field, $path);
}
I just confirmed that MariaDB does not support the -> extraction operator as an alias to the JSON_EXTRACT() function. However, the same query works against a vanilla MySQL 5.7 server.
Assuming this test table:
╔════╤══════════════════╗
║ id │ payload ║
╟────┼──────────────────╢
║ 1 │ {"a": 1, "b": 2} ║
╚════╧══════════════════╝
A query that uses the -> extraction operator:
SELECT payload->"$.b" FROM test;
fails against MariaDB 10.2.8 while it yields a correct 2 against a MySQL 5.7.19 server.
Solutions
The right solution depends on what you're using on production.
Replace MariaDB
If you're using MySQL, replace MariaDB with MySQL in your development env. On a macOS machine managed by homebrew, it'd be as easy as:
brew services stop mysql
brew uninstall mariadb
brew install mysql
brew services start mysql
your data will remain intact.
Rewrite your queries
However, if you're using MariaDB in production, you need to rewrite your queries to use JSON_EXTRACT() function as Elias already mentioned. As you can see you need to be much more verbose with the Laravel API.
The above query would be:
SELECT JSON_EXTRACT(payload, "$.b") FROM test;
I made Laravel MariaDB driver for json support. Get it here: ybr-nx/laravel-mariadb
The problem is that only MySQL supports the -> operator, then the query will fail on MariaDB, but:
Both MariaDB and MySQL supports the JSON_EXTRACT function, that parses the JSON and get a value from it.
You can solve it by doing a query like this:
SELECT * FROM notifications WHERE JSON_EXTRACT(`notifications.data`, "$.id") = 5
To do it with Laravel's Query Builder, you'll need to use DB::raw method:
DB::table('notifications')->where(DB::raw('JSON_EXTRACT(`notifications.data`, "$.id")'), '=', 3);
Please try it and tell me if go wrong.
Note: The JSON_EXTRACT() is only available on MySQL >= 5.7 or MariaDB >= 10.2.3
Thanks to #Sepehr, I didn't know that the -> was an MySQL Operator, neither that JSON type existed. :-)
The issue is with the ->, i think you have a var called $data with some collection in it.
If that so then the correct way is:
DB::table('notifications')->where($data->id, '3')->get();
Or if you have a Model related to notifications table then:
Notification::where($data->id, '3')->get();
If the Model is called Notification (following Eloquent convention)
But if you tried to find all ID's equals 3 then just:
DB::table('notifications')->where('id', '3')->get();
I'm new in cassandra and I wanna get the values from a column family, where the key is a TimeUUIDType.
I'm using PHP with PHPCassa, and I can insert(set) correctly in the column family, generating uuid with the function:
$key = CassandraUtil::uuid1();
The problem happen when I try to do a get in the column family, because i have and uuid in String format( something like that):
$uuidString= "e2658820-69f2-11e1-af9a-95dd4f324d9";
I would like to know if is possible cast or transform an String form to a valid uuid for cassandra in php or phpcassa, because my purpose is in another page create the correct uuid from the $uuuidString.
Thanks.
CassandraUtil::import($uuidString) will handle that.
Edit
As of phpcassa 1.0+, \phpcassa\UUID::import() is the proper method.