Mysql从表中获取所有数据并计算一些列的数量

时间:2016-10-05 11:11:53

标签: mysql

有表table 1结构,如:

`id, rma_id, order_id, product_id, order_nr, comment, admin_comment, add_date`

及其数据:

(40, 1, 110331, 4399, 1, 'comment1', '', '2016-09-30 08:46:54'),
(42, 2, 110331, 4399, 1, 'comment2', '', '2016-09-30 11:18:06'),
(43, 3, 110374, 4399, 1, 'comment3', '', '2016-10-03 05:55:25'),
(44, 4, 110374, 4399, 1, 'comment4', '', '2016-10-03 05:55:43'),
(45, 4, 110374, 4399, 2, 'comment5', '', '2016-10-03 05:55:43');

使用这样的查询来获得所有精确RMA:

SELECT * FROM `rma_products` WHERE `rma_id` = 4

得到这样的结果:

[0] => array(8) {
    ["id"] => string(2) "44"
    ["rma_id"] => string(1) "4"
    ["order_id"] => string(6) "110374"
    ["product_id"] => string(4) "4399"
    ["order_nr"] => string(1) "1"
    ["comment"] => string(16) "comment4"
    ["admin_comment"] => string(0) ""
    ["add_date"] => string(19) "2016-10-03 08:55:43"
}
[1] => array(8) {
    ["id"] => string(2) "45"
    ["rma_id"] => string(1) "4"
    ["order_id"] => string(6) "110374"
    ["product_id"] => string(4) "4399"
    ["order_nr"] => string(1) "2"
    ["comment"] => string(7) "comment5"
    ["admin_comment"] => string(0) ""
    ["add_date"] => string(19) "2016-10-03 08:55:43"
}

但是我想添加一个像count这样的字段,我的结果会像这样:

[0] => array(8) {
    ["id"] => string(2) "44"
    ["rma_id"] => string(1) "4"
    ["order_id"] => string(6) "110374"
    ["product_id"] => string(4) "4399"
    ["order_nr"] => string(1) "1"
    ["comment"] => string(16) "comment4"
    ["admin_comment"] => string(0) ""
    ["add_date"] => string(19) "2016-10-03 08:55:43"
    ["count"] => string(1) "2"
}
[1] => array(8) {
    ["id"] => string(2) "45"
    ["rma_id"] => string(1) "4"
    ["order_id"] => string(6) "110374"
    ["product_id"] => string(4) "4399"
    ["order_nr"] => string(1) "2"
    ["comment"] => string(7) "comment5"
    ["admin_comment"] => string(0) ""
    ["add_date"] => string(19) "2016-10-03 08:55:43"
    ["count"] => string(1) "2"
}

将根据相同的product_id在同一个rma_id

中的数量来计算

1 个答案:

答案 0 :(得分:1)

 <?xml version="1.0" encoding="utf-8" ?>
 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="App2.ParentHomePage">

 <ContentPage.Content>
   <StackLayout>
    <StackLayout.Children>
     <Frame OutlineColor="Accent">
       <Frame.Content>
         <StackLayout Orientation="Horizontal">
          <StackLayout.Children>
            <Label Text="Home Page"
                   VerticalOptions="Center" />
          </StackLayout.Children>
         </StackLayout>
       </Frame.Content>
     </Frame>

  </StackLayout.Children>
</StackLayout>
</ContentPage.Content>
</ContentPage>