简体   繁体   中英

zend framework 2 Db Select columns Grouping

I need a query to my database

This is my database data

CREATE TABLE IF NOT EXISTS `order_main` (
`om_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`om_raren` varchar(20) NOT NULL COMMENT 'serial number',
`om_total` int(11) NOT NULL COMMENT 'money',
`om_freight` int(11) NOT NULL COMMENT 'freight',
`om_amount` int(11) NOT NULL COMMENT 'money + freight',
`om_status` int(11) NOT NULL COMMENT 'Order Status:1)Unpaid 2)Paid 3)Cancel 4)Payment     
success,
`om_created` datetime NOT NULL COMMENT 'create time',
PRIMARY KEY (`om_id`)
)

My needs,Obtain 4 kinds of data

1) Not specified 'om_status' , 'om_created' = 2013-01 Items Count

2) Not specified 'om_status' , 'om_created' = 2013-01' sum('om_total')

3) 'om_status' = 4 , 'om_created = 2013-01' Items Count

3) 'om_status' = 4 , 'om_created' = 2013-01' sum('om_total')

First and second, and I know how to write,like this:

$select = $this->tableGateway->getSql()->select();
$select->columns(array(
            'Nub' => new \Zend\Db\Sql\Expression('COUNT(*)'),
            'Price' => new \Zend\Db\Sql\Expression('sum(`om_total`)'),
        ));
$select->where("DATE_FORMAT(`om_created`, '%Y-%m')  = 2013-01");
$row = $this->tableGateway->selectWith($select)->toArray();

But the third and fourth, I don't know how clueless adding that Sql Query, i want 4 kinds of results in the same sql string

Thanks

Just add another where() :

...
$select->where("DATE_FORMAT(`om_created`, '%Y-%m')  = 2013-01");
$select->where("om_status = 4");
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM