creating command in yii for mysql - 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.

Related

Is is possible to use between and like with JSON in mysql / mariadb?

I have a table name data with a column name body containing records present in json format i.e.,
'{"createdOn": "2018-10-05T00:00:00.000+0000","id":1,"name":"abc"}',
'{"createdOn": "2018-10-06T00:00:00.000+0000","id":2,"name":"xyz"}',
'{"createdOn": "2018-10-10T00:00:00.000+0000","id":3,"name":"aaa"}',
'{"createdOn": "2018-10-25T00:00:00.000+0000","id":4,"name":"qqq"}',
I am looking to fetch and delete the records between "createdOn":"2018-10-05T00:00:00.000+0000" and "createdOn":"2018-10-10T00:00:00.000+0000" for which I am trying to write a query SELECT * FROM data where body like between '%"createdOn": "2018-10-05%' and '%"createdOn": "2018-10-10%'; but it is giving the error . How can something like this be achieved ? If anybody can help ?
Thanks
Yes, you can extract values from json and use them in queries:
select * from data where JSON_VALUE(body, '$.createdOn') between '2018-10-05T00:00:00.000+0000' and '2018-10-10T00:00:00.000+0000';
select * from data where JSON_VALUE(body, '$.name') LIKE '%a%';

How can we split the strings in mysql IN query?

I'm trying to fetch the data in NodeJs using mysql IN query.
I'm getting the data dynamically and need to get data based on textValues;
For example , let textValues = 'a1b, a2b, a3b, a4b' and my query is-
select * from table where values IN (textValues).
but this will not generate the expected output.
As I'll achieve this using
select * from table where values IN ('a1b','a2b','a3b','a4b').
i.e. textValues should be in 'a1b','a2b' which we can't store in any data types like this.
How can we achieve this with MYSQL IN query.
Thank You

how to get a particular field from django model

I want a command for getting a query set from the model.
The SQL command will be like
SELECT teamName FROM TABLE WHERE userName=userName;
I found the answer
teamNameQuery=userInfo.objects.filter(userName=userName).values('teamName').first()
you can retrive list of field from model by using values_list
Model.Objects.filter(userName = userName).values_list('teamName')

Converting ActiveRecord::Result to ActiveRecord::Base

I have running an SQL query, which ends up returning * from table ABC.
I am running this in my ruby on rails code by below command:
query:
sql = select * from ABC WHERE <condition>
results = ActiveRecord::Base.connection.exec_query(sql)
I am getting the outputs as results which is of type ActiveRecord::Result
This, I am converting to an array, by using function to_hash provided by ActiveRecord::Result. However, this is an array of Hashes.
Is there a way in which I can convert it to an array of ActiveRecord's
(I need to do further processing with each active record)
For ex: single_result.outdated? (where outdated is a field belonging to another table DEF which is connected to table ABC via single_result.id)
Any help is appreciated. Thanks!

Database - getting multi dimensional data

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