Database - getting multi dimensional data - mysql

Is there some command in mysql or nosql databases so i could get data sorted in multidimensional array by some sort field?
For example : SELECT * FROM TABLE ARRAYSORT field1
so we would get
array('field1_value1'=>array(data),'field1_value2'=>array(data))
or we must process data in our programming language to get array like this.
It is just imposible that no database support query like this.

I don't know if that's what you want to do:
PHP:
$result = $polaczenie->query($sql)
while ($row = $result->fetch_array()){
$array[$Row['firstColumnToSort']][$Row['secondColumnToSort']]=data;
}
Or something like that

Related

Laravel mongodb where array field contains value

I use mongodb and mysql together with Laravel.
Fields table in mysql database can have more options in mysql database.
fields table in mysql: id, name
options table in mysql: id, field_id, name
so in mongodb database I have adverts table and this adverts table have field and field equal to options
something like this for example {field_id_1: [option_id, option_id]}. When I use it like this there is no problem when querying the database. It is simple like this $advert->whereIn('field_id_1', $request->options).
but then I decided why not to keep this options as an array in mongodb and without field_id like this {options: [option_id, option_id, option_id]}. So there is the question now I somehow want query the database where this options field contains requests value something reverse of $adverts->whereIn('field', $options)
I know a way but not sure
$adverts->where('options', 'LIKE', '%"'.$option_id.'"%');
I don't know if this answer will work for you :
$result = $adverts->where("options", "all", [$option_id])->get();
or :
$result = $adverts->where("options", "all", "$options_ids")->get();
where $options_ids are an array of options

How to retrieve data from json column using codeigniter?

I’m a beginner with codeigniter. I’m trying to retrieve data from json column of my sql db
The db structure is like this
DB NAME : order
——————————————————------------------------------------------
Id | description
——————————————————————————————————---------------------------
1 | {“pc”: [{ “brand”: “name”}], “mouse”: [“LL”, “DC”]}
————————————————————————————————————--------------------------
For example, I want to retrieve all instances that has mouse = dc
$data = $this->db->select($select)->from(‘order’)
->where(“mouse”, “DC”) ->get()->result();
Well, I've never worked like this before, but with the help of the user ascsoftw answer, I've found this section on the MySQL documentation that might help, I think this is even better for you case.
Then, your query may look something like this (sorry if I'm mistaken, like I said, never worked like this before):
SELECT Id FROM order where description->>"$[1][1]";
From what I understand that would retrieve all the Id's of the mouse (array index 1) with DC's description (array index 1 as well).
You may keep reading the docs of MySQL if that didn't help.
By the way,is worth mention that, you have several ways to do queries in CodeIgniter, if the CI query builders does not adapt to what you want to do, you can always do as the following example:
$query = "SELECT * FROM table_name";
$result = $this->db->query($query);
if($result->num_rows() != 0)
{
foreach($result->result() as $row)
{
return something
}
}
else
{
return false
}
Let me know if that helped you so I can edit with the correct answer for anyone running into the same problem.

Laravel Query Builder join on json data column

On my database, I have a notifications table that has a data column containing a JSON string like this one:
{"sender_id":2,"sender_name":"John Doe","message":"Hello world!"}
What I need is to join the users table on users.id = notifications.data.sender_id but I don't know how or if it's even possible. Couldn't find any info regarding this kind of query.
Looking forward to reading your solutions. Thanks!
You could do a little hacky solution which is this:
$id = 123;
YourModel::where('json_data_column', 'like', '%"sender_id":' . $id . ',%');
You are basically doing a string search in the JSON. However, there is also a JSON column type in MySQL, which allows better support for JSON searches. You can then do:
$id = 123;
YourModel::where('json_data_column->sender_id', $id);

creating command in yii for mysql

How to write or create command as
select * from profile where name like r% limit 12,16;
in mysql to get the appropriate result in yii.
please any help would be appreciated. thank you in advance
You should use this code:
$queryResult = Yii::app()->db->createCommand('select * from profile where name like "r%" limit 12,16;')->queryAll();
Result will be an array of associative arrays where keys will represents column names, and values are values fetched from DB.

Rails 3 connection.execute IN statement parameters array syntax

I have a syntax question regarding the Rails 3 ActiveRecord::Base.connection.execute method and parameter that i want to use with it. I have been fighting with this for some hours now and just do not seem to find any answers to this specific question on the internet.
The database is MySQL.
I need to create a temporary table through SELECT with IN condition statement, where the list of values against which IN should check, is a parameter - rails Array. The code looks like so:
arr = [1,2,3]
ActiveRecord::Base.connection.execute("CREATE TEMPORARY TABLE things SELECT * FROM objects WHERE objects.id IN #{arr}")
I get a MySQL syntax error!
ActiveRecord::Base.connection.execute("CREATE TEMPORARY TABLE things SELECT * FROM objects WHERE objects.id IN #{(arr)}")
Again MySQL syntax error!
ActiveRecord::Base.connection.execute("CREATE TEMPORARY TABLE things SELECT * FROM objects WHERE objects.id IN (#{arr})")
MySQL syntax error
The above attempts correspond to this question-answer:
How to execute arbitrary parameterized SQL in rails
I even tried to use in the manner like with find_by_sql, but still get an error:
ActiveRecord::Base.connection.execute(["CREATE TEMPORARY TABLE things SELECT * FROM objects WHERE objects.id IN (:ids)",{:ids => arr }]) - obviously, I get an MySQL error.
Am I missing something obvious? Please help! I need this exactly in this way (e.g. create temporary table with exactly such conditions), otherwise a more complicated query based on this one will not work. Thanks!
Here's another option that uses Rails' query sanitization
arr = [1,2,3]
query = "SELECT * FROM objects where id IN (?)"
query = ActiveRecord::Base.send :sanitize_sql_array, [query, arr]
ActiveRecord::Base.connection.execute(query)
If you convert arr.to_s you get "[1, 2, 3]".
I think arr.join(', ') should work.
ActiveRecord::Base.connection.execute("CREATE TEMPORARY TABLE things SELECT * FROM objects WHERE objects.id IN (#{arr.join(', ')})")