How to get max and min date in Yii 2 - yii2

In my databae
filed type
'import_date varchar(100)'
value
'max => 01-01-2016'
'min => 31-12-2015'
I have code to get max and min date
$dateMin = Report::find()->min('import_date');// ouput 01-01-2016' => false
$dateMax = Report::find()->max('import_date');// ouput 31-12-2015' => false
Please help me
Thank all

You should find the min of the converted field in date
$dateMin = Report::find()->min("STR_TO_DATE(import_date,'%d-%m-%Y' ");
$dateMax = Report::find()->max("STR_TO_DATE(import_date,'%d-%m-%Y' ");

Related

how to insert array to a field in db - wordpress

I want to insert values to database each time when values are submitted.
These are the post values from the form.
$sFloorKey = $_POST['floorname']; // int value : eg - 5
$aRoomName = $_POST['roomname']; //array contaning multiple values
$iRoomType = $_POST['roomtype']; //array contaning multiple values
$iPostID = $_POST['postid']; // int value : eg - 44
The wp query performed ,
$wpdb->insert('bs_room_types', array(
'postid' => $iPostID ,
'sfloorname' => $sFloorKey,
'aroomname' => $aRoomName,
'aroomtypes' => $iRoomType,
));
The $sFloorKey and $iPostID are getting inserted but the array values not.
Please help out here!
As per the help of #MilanChheda its working perfectly now.
Before inserting i have serialized the values.
$iPostID = $_POST['postid'];
$sFloorKey = $_POST['floorname'];
$aRooms = serialize($_POST['roomname']);
$iRoomType = serialize($_POST['roomtype']);
$wpdb->insert('bs_room_types', array(
'postid' => $iPostID,
'sfloorname' => $sFloorKey,
'aroomname' => $aRooms,
'aroomtypes' => $iRoomType,
));

Laravel how to match 2 values with one field

Currently I am facing problem that I am using orWhere to get the correct result but not getting the exact result. I want to get result where my field contain only Paid and Flexible
E.g
Status
Paid
Paid
Flexible
Paid
Paid
Flexible
But with this query I am also getting UnPaid but I don't want UnPaid result
$orders = Order::with('orderfiles','orderStatus','orderDelivery','flexibleDelivery')->whereHas('payment', function ($query) {
$query->where("status","=",'Paid')->orwhere('status' ,'=', 'Flexible');
})->whereBetween("tbl_orders.order_date", $range)->where('domain_id','=',$domain_id)->orderBy('order_date','asc')->paginate();
return View('admin.all_orders')
->with('orders',$orders)->with('title','Results - Paid and Flexible Orders')->with('logoNum',$domain_id);
Sql
Array
(
[0] => select count(*) as aggregate from `tbl_orders` where (select count(*) from `tbl_payments` where `tbl_payments`.`order_id` = `tbl_orders`.`order_id` and `status` = ? or `status` = ?) >= 1 and `tbl_orders`.`order_date` between ? and ? and `domain_id` = ?
[1] => Array
(
[0] => Paid
[1] => Flexible
[2] => 2015-01-01
[3] => 2015-05-31
[4] => 18
)
[2] => 7233.41
[3] => mysql
)
I know you’ve already accepted an answer, but it’s really not the best way. You could use Laravel’s whereIn query method:
$query->whereIn('status', ['Paid', 'Flexible']);
The first parameter is the column name, and the second is an array of values you want to match.
Use Db::RAW
for more help read: http://laravel.com/docs/4.2/queries
$orders = Order::with('orderfiles','orderStatus','orderDelivery','flexibleDelivery')->whereHas('payment', function ($query) {
$query->where(DB::raw('(`status` = \'Paid\') OR (`status` = \'Flexible\')'));
})->whereBetween("tbl_orders.order_date", $range)->where('domain_id','=',$domain_id)->orderBy('order_date','asc')->paginate();
return View('admin.all_orders')
->with('orders',$orders)->with('title','Results - Paid and Flexible Orders')->with('logoNum',$domain_id);

Cakephp find joining two columns

I have a BD that contains people with day and month fields, like so:
person1 (day = 5; month = 3)
person2 (day = 2; month = 12)
I have to perform a find between 2 dates, for example, I need all people between 01/03 and 01/06 (day/month) but I don't know how to perform that.
I tried using separate conditions, like this:
$conditions['People.day >='] = dayA;
$conditions['People.month >='] = monthA;
$conditions['People.day <='] = dayB;
$conditions['People.month <='] = monthB;
But, that's not correct because it finds day and month, I mean, it finds People between monthA and monthB and People between dayA and dayB, instead, what I need is people between dayA/monthA and dayB/monthB
I suppose I must do some kind of a JOIN, but I'm lost here, I looked some information but I don't know where to start.
updated info:
ok, I'm using this
Array
(
[People.month_day BETWEEN ? AND ?] => Array
(
[0] => 01/02
[1] => 28/02
)
)
but I get people like this:
1/1
2/1
10/1
11/1
12/1
13/1
20/1
21/1
1/2
what's wrong? do you need more code? I'm using this to retrieve results:
A paginate:
public $paginate = array('People'=>array(
'limit' => 16,
'order' => 'People.month, People.day ASC'
));
In your People model
public $virtualFields = array(
'month_day' => 'CONCAT(People.month, "-", People.day)'
// 'month_day' => 'CONCAT(People.day, "/", People.month)'
);
then add in your controller
$options = array(
'conditions' => array(
'Post.month_day BETWEEN ? AND ?' => array('01-03','01-06')
// 'Post.month_day BETWEEN ? AND ?' => array('03/01','06/01')
)
);
$posts = $this->Post->find('all',$options);
It would be best to alter your table. Use DATE field and your SQL query would be:
SELECT *
FROM dbname.People
WHERE People.date BETWEEN '1999-03-01' AND '1999-06-01';

Group by query returning only latest result - Codeigniter

I have the following query, where I want to retrieve the earliest/latest date for each group.
Each group can have many members who are travelling at different dates, so I want to know what is the earliest date within that group between those members. Similarly this is done with all existing groups and then displayed in the chosen order.
This is the query I'm using:
public function sortByDate($order, $year)
{
$this->db->select('groups.*, general_info.proposed_date_of_arrival');
$this->db->from('groups');
$this->db->join('general_info', 'general_info.group_id = groups.group_id');
$this->db->where('YEAR(groups.group_year)', $year);
$this->db->group_by('groups.group_id');
$this->db->order_by("general_info.proposed_date_of_arrival", $order);
$val = $this->db->get();
}
But the above only returns and compares the latest/most recent entries to each group, which isn't the type of query that should be generated.
An output of the results:
array (size=2)
0 =>
array (size=5)
'group_id' => string '2' (length=1)
'group_leader_name' => string 'Bob' (length=7)
'group_year' => string '2013-11-01' (length=10)
'year' => string '2013' (length=4)
'proposed_date_of_arrival' => string '2014-02-22' (length=10)
1 =>
array (size=5)
'group_id' => string '1' (length=1)
'group_leader_name' => string 'John' (length=7)
'group_year' => string '2013-11-12' (length=10)
'year' => string '2013' (length=4)
'proposed_date_of_arrival' => string '2014-12-15' (length=10)
There are actually two members each in these two test groups, the earliest in group 1 having a proposed date of arrival of 2013-12-31 and the earliest in group 2 being 2014-01-19.
The actual output of ordering in ascending order should be the group with the date of 2013-12-31 being first, and the group with the date of 2014-01-19 being next. However, I get the above array result instead, which is comparing the two most recent members instead of taking into account all the members proposed dates. How can I compare between all the members dates?
I think this is the solution
public function sortByArrivalDateASC($year)
{
$this->db->select('groups.*, MIN(date(general_info.proposed_date_of_arrival)) as proposed_date_of_arrival');
$this->db->from('groups');
$this->db->join('general_info', 'general_info.group_id = groups.group_id');
//$this->db->where('YEAR(groups.group_year)', $year);
$this->db->group_by('group_id');
$val = $this->db->get();
}
I was missing an aggregate function to group by. I figured I don't really need to implement a descending order, since we only want to know the earliest time of departure.
Thanks to GROUP_BY MySQL reference and this w3schools explanation

Count number of rows with distinct column

I'm trying to count the number of rows with a distinct column with Active Record.
The following SQL works and gives me the correct result:
SELECT COUNT(DISTINCT(user_id)) FROM attempts WHERE score = 100 AND problem_id = 1
But this code throws an ActiveRecord::StatementInvalid error:
attempts.where(:score => 100).count(:distinct => :user_id)
Try:
attempts.count('user_id', :conditions => "score = 100", :distinct => true)
more infos: http://ar.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html