通过将列分组返回特定​​的行

时间:2018-09-12 06:55:44

标签: mysql sql

我坚持使用SQL查询,其想法是我有一个数据库,我将所有文档与每个文档version(revisionNumber)等一起保存。我要实现的目标是,我目前只想访问那些文档中包含最新的revisionNumber

|  id | title               | documentForm | revisionNumber | effectiveDate |
| --: | ------------------- | -------------| -------------: | :------------ |
|   1 | Event Calendar      | SOP-CL       |            1.0 | 2011-02-02    |
|   2 | Event Calendar      | SOP-CL       |            2.0 | 2012-12-16    |
|   3 | Event Calendar      | SOP-CL       |            3.0 | 2014-02-15    |
|   4 | Event Calendar      | SOP-CL       |            4.0 | 2014-08-01    |
|   5 | Event Calendar      | SOP-CL       |            5.0 | 2016-09-12    |
|   6 | Event Calendar      | SOP-CL       |            6.0 | 2018-09-11    |
|   7 | Software development| SOP-DEV      |            1.0 | 2015-11-25    |
|   8 | Granting and...     | SOP-GRA      |            1.0 | 2014-08-04    |
|   9 | Granting and...     | SOP-GRA      |            2.0 | 2015-12-07    |
|  10 | Granting and...     | SOP-GRA      |            3.0 | 2018-03-26    |

在这里您可以看到查询后我需要获得的结果:

|  id | title               | documentForm | revisionNumber | effectiveDate |
| --: | ------------------- | ------------ | -------------: | :------------ |
|   6 | Event Calendar      | SOP-CL       |            6.0 | 2018-09-11    |
|   7 | Software development| SOP-CL       |            1.0 | 2015-11-25    |
|   3 | Granting and...     | SOP-GRA      |            3.0 | 2018-03-26    |

我一直在Google中进行搜索,发现可以通过对文档进行分组(例如-documentForm)并返回MAX(revisionNumber)来完成,但是我没有得到正确的行id和{{1} }。我想我只是不正确地使用它们。

3 个答案:

答案 0 :(得分:2)

使用相关子查询

arc

答案 1 :(得分:2)

您可以尝试在where子句中使用子查询。

模式(MySQL v5.6)

CREATE TABLE t (
  title varchar(50),
  documentForm varchar(50), 
  effectiveDate date,
  revisionNumber int

);

insert into t values ('vent Calendar','SOP-CL','2011-02-02',1.0);
insert into t values ('vent Calendar','SOP-CL','2012-12-16',2.0);
insert into t values ('vent Calendar','SOP-CL','2014-02-15',3.0);
insert into t values ('vent Calendar','SOP-CL','2014-08-01',4.0);
insert into t values ('vent Calendar','SOP-CL','2016-09-12',5.0);
insert into t values ('vent Calendar','SOP-CL','2018-09-11',6.0);
insert into t values ('oftware development ','SOP-DEV','2015-11-25',1.0);  
insert into t values ('ranting and..','SOP-GRA','2014-08-04',1.0);
insert into t values ('ranting and..','SOP-GRA','2015-12-07',2.0);
insert into t values ('ranting and..','SOP-GRA','2018-03-26',3.0);

查询#1

SELECT * 
FROM t t1
WHERE revisionNumber  = (
   select max(tt.revisionNumber)
   from t tt
   WHERE t1.documentForm = tt.documentForm
);

| title                | documentForm | effectiveDate | revisionNumber |
| -------------------- | ------------ | ------------- | -------------- |
| vent Calendar        | SOP-CL       | 2018-09-11    | 6              |
| oftware development  | SOP-DEV      | 2015-11-25    | 1              |
| ranting and..        | SOP-GRA      | 2018-03-26    | 3              |

View on DB Fiddle

答案 2 :(得分:1)

具有一个子查询,该查询执行function discount_table(){ $cat_in_cart = false; $tot_cat = 0; foreach ( WC()->cart->get_cart() as $cart_item ) { if ( has_term( 'cat1', 'product_cat', $cart_item['product_id'] ) ) { $cat_in_cart = true; $tot_cat_price = $cart_item['data']->get_price(); $tot_cat_qty = $cart_item['quantity']; $tot_cat += $tot_cat_price * $tot_cat_qty; //break; /* commented here, thanks to Michael Ramin: break will stop execution of the loop after found the first product belonging to the category; what if you have more than one? so remove break instruction */ } } global $product; $total = WC()->cart->subtotal; $discount_label = ""; if($total >= 300){ $discount_label=15; }elseif($total >= 220){ $discount_label=10; }elseif($total >= 150){ $discount_label=8; }elseif($total >= 100){ $discount_label=4; } $chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); $chosen_shipping = explode(':',$chosen_methods[0]); if($chosen_shipping[0]=='local_pickup'){ $discount_label=25; } $discount_brand = ($total-$tot_cat)*$discount_label/100; if($discount_label!=""){ $discount_brand_net = ($discount_brand/1.1); if($discount_brand_net != 0) // show discount only if != 0; it may happen if cart includes only targeted category products; WC()->cart->add_fee( "Discount ($discount_label%)", -$discount_brand_net, false ); } } add_action( 'woocommerce_cart_calculate_fees','discount_table' ); 返回具有最高修订版本号的每个documentForm。 GROUP BY得到的结果:

JOIN