如何在Mysql中检索类似的订单?

时间:2019-01-20 05:23:23

标签: mysql sql database

我需要一个查询,该查询应首先查找状态为0(零)的最旧订单。并检索该类型的所有相似订单(匹配确切的总数量,itemSku和订购的不同项目数)。

***OrdersTable***
ID    OrderNumber    CustomerId    Status    created_at
1     123456         1             0         2018-01-01 
2     234567         1             0         2018-01-02
3     345678         1             0         2018-01-03
4     456789         1             0         2018-01-04   

***PurchasedProductsTable***
OrderId     itemSku     Qty
1           1000001     1
1           1000002     2
2           1000001     3
3           1000001     1
3           1000002     2
4           1000001     3

在上表中,查询应首先查看状态为0的最旧(created_at ASC)订单(即ID为1)(在订单表中)。并应与该订单一起检索与同一itemSku,数量和不同商品总数相匹配的所有其他订单(在PurchaseProducts表中)。

这里订单1和3匹配相同的项目SKu(1000001和1000002)和数量(1和2)都分别具有(2)个不同的项目计数,因此应首先检索订单1和3。 1和3出厂(即更改为2)。

,如果我再次运行查询,它应该检索类似的命令。现在,订单2和4就像订单2和4一样。 (具有相同的itemSkus(1000001,数量(3),不同的项目计数(1))。

请帮助谢谢

2 个答案:

答案 0 :(得分:1)

您必须去两次餐桌:)

类似这样的东西:

SELECT DISTINCT O2.ID
FROM OrdersTable O1
INNER JOIN PurchasedProductsTable P1 ON O1.ID = P1.OrderId
INNER JOIN PurchasedProductsTable P2 ON P1.itemSku = P2.itemSku 
  AND P1.Qty = P2.Qty 
INNER JOIN OrdersTable O2 ON O2.ID = P2.OrderId
WHERE O1.ID = 
        (SELECT ID FROM OrdersTable WHERE Status = 0 
              ORDER BY created_at ASC LIMIT 1)
  AND (SELECT COUNT(*) FROM PurchasedProductsTable WHERE OrderId = O1.ID)
     = (SELECT COUNT(*) FROM PurchasedProductsTable WHERE OrderId = O2.ID)
ORDER BY O2.ID ASC;

https://www.db-fiddle.com/f/65t9GgSfqMpzNVgnrJp2TR/2

答案 1 :(得分:0)

您可以通过限制并按日期获得最早的订单。
然后,您可以离开联接以获取该订单以及至少具有相同项目的任何其他订单。

然后,一旦从子查询结果中获得了这些订单ID,就可以获取订单详细信息。

import 'package:flutter/material.dart';

import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

class StaggeredGridExample extends StatefulWidget {
  @override
  _StaggeredGridExampleState createState() => _StaggeredGridExampleState();
}

class _StaggeredGridExampleState extends State<StaggeredGridExample> {
  final List<String> images = [
    "https://uae.microless.com/cdn/no_image.jpg",
    "https://images-na.ssl-images-amazon.com/images/I/81aF3Ob-2KL._UX679_.jpg",
    "https://www.boostmobile.com/content/dam/boostmobile/en/products/phones/apple/iphone-7/silver/device-front.png.transform/pdpCarousel/image.jpg",
    "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSgUgs8_kmuhScsx-J01d8fA1mhlCR5-1jyvMYxqCB8h3LCqcgl9Q",
    "https://ae01.alicdn.com/kf/HTB11tA5aiAKL1JjSZFoq6ygCFXaw/Unlocked-Samsung-GALAXY-S2-I9100-Mobile-Phone-Android-Wi-Fi-GPS-8-0MP-camera-Core-4.jpg_640x640.jpg",
    "https://media.ed.edmunds-media.com/gmc/sierra-3500hd/2018/td/2018_gmc_sierra-3500hd_f34_td_411183_1600.jpg",
    "https://hips.hearstapps.com/amv-prod-cad-assets.s3.amazonaws.com/images/16q1/665019/2016-chevrolet-silverado-2500hd-high-country-diesel-test-review-car-and-driver-photo-665520-s-original.jpg",
    "https://www.galeanasvandykedodge.net/assets/stock/ColorMatched_01/White/640/cc_2018DOV170002_01_640/cc_2018DOV170002_01_640_PSC.jpg",
    "https://media.onthemarket.com/properties/6191869/797156548/composite.jpg",
    "https://media.onthemarket.com/properties/6191840/797152761/composite.jpg",
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StaggeredGridView.countBuilder(
        crossAxisCount: 4,
        itemCount: 10,
        itemBuilder: (BuildContext context, int index) => Card(
          child: Column(
            children: <Widget>[
              Image.network(images[index]),
              Text("Some text"),
            ],
          ),
        ),
        staggeredTileBuilder: (int index) =>
        new StaggeredTile.fit(2),
        mainAxisSpacing: 4.0,
        crossAxisSpacing: 4.0,
      ),
    );
  }
}

RexTester here

上进行测试