LEFT JOIN和COUNT

时间:2016-01-15 12:29:13

标签: php mysql join

我有三张桌子如下:
1.化妆品以cosm_id为主键,AI,cosm_name是唯一的。

| cosm_id | cosm_brand | cosm_name | cosm_volume 

2。 cosmInOut使用cosmInOut_id作为主键,AI,外键作为名称cosmInOut_qtyIn始终为1

| cosmInOut_id | cosmInOut_name | cosmInOut_qtyIn | cosmInOut_4sale

3。 cosmSpent,其中cosmSpent_id为主键,AI,外键为名称。 cosmSpent_volume是cosm_volume的一部分

 | cosmSpent_id | cosmSpent_name | cosmSpent_volume

我想要的是为每个cosm_name创建一个汇总表,每个品牌的列为:

Name = cosm_name (DISTINCT)  

Quantity in stock = (SUM(cosm_volume) - SUM(cosmSpent_volume)) / cosm_volume

当前价格=这里我甚至不知道如何解决当前使用的行的价格。例如。如果cosmInOut表有3行所需的cosm_name,其中cosm_volume = 100,而SUM(cosmSpent_volume)= 150,我必须在CosmInOut表的第二行找到当前价格的cosm_name。我该怎么做?

到目前为止,我设法创建了这个查询:

$sql = "SELECT cosmInOut_id, cosm_brand, cosm_name, cosm_volume, cosmInOut_priceIn, 
                       SUM(cosm_volume) AS cosm_volume_total,
                       SUM(cosmSpent_volume) AS cosmSpent_total
        FROM cosmInOut
        JOIN cosmetics ON cosmInOut.cosmInOut_name=cosmetics.cosm_name
        LEFT JOIN cosmSpent ON cosmInOut.cosmInOut_name=cosmSpent.cosmSpent_name
        WHERE cosmInOut_4sale = '0' AND cosm_brand = '" . $row_brand['cosm_brand']. "'
        GROUP BY cosm_name";

在一个循环中对每个cosm_brand执行 不幸的是,由于(我认为)使用LEFT JOIN,因此该查询中的数学运算完全错误。

我有两个问题: 1.如何修复查询以正确计数 2.如何根据计数值

寻址N行

非常感谢您的帮助。我是新手,所以不要严格判断。如果问题出在架构上,我愿意接受建议

样本数据:
enter image description here

对所需结果的一点解释:

Total cosmInOut_volume is 60*3 = 180,  
total cosmSpent volume = 10+55+5+10 = 80,  
thus qty in stock = (180-80)/60 = 1.66. 

目前,第二行用于确定价格,因为我们已经使用了一整行(60)并开始使用第二行

这是MySQL:http://sqlfiddle.com/#!9/fdfad9/1

    CREATE TABLE cosmetics
    (`cosm_id` int, `cosm_brand` varchar(50), `cosm_name` varchar(50), `cosm_volume` int)
;

INSERT INTO cosmetics
    (`cosm_id`, `cosm_brand`, `cosm_name`, `cosm_volume`)
VALUES
    (1, 'Londa', 'L 5/66', '60')

;

CREATE TABLE cosmInOut
    (`cosmInOut_id` int, `cosmInOut_name` varchar(50), `cosmInOut_qty` int, `cosmInOut_priceIn` float, `cosmInOut_4sale` tinyint)
;

INSERT INTO cosmInOut
    (`cosmInOut_id`, `cosmInOut_name`, `cosmInOut_qty`, `cosmInOut_priceIn`, `cosmInOut_4sale`)
VALUES
    (1, 'L 5/66', '1', '7', '0'),
    (2, 'L 5/66', '1', '10', '0'),
    (3, 'L 5/66', '1', '7', '0')

;

CREATE TABLE cosmSpent
    (`cosmSpent_id` int, `cosmSpent_name` varchar(50), `cosmSpent_volume` int)
;

INSERT INTO cosmSpent
    (`cosmSpent_id`, `cosmSpent_name`, `cosmSpent_volume`)
VALUES
    (1, 'L 5/66', '10'),
    (2, 'L 5/66', '55'),
    (3, 'L 5/66', '5'),
    (4, 'L 5/66', '10')

;

1 个答案:

答案 0 :(得分:0)

<强> SQL Fiddle Demo

要产生你想要的结果,你需要创建三个查询并将它们连接在一起。

  • 首先,您需要知道每个化妆品名称的含量是多少,以及为这些名称购买的商品数量
  • 然后计算每个名字的总支出
  • 使用变量计算体积范围和相应范围的价格

SELECT v_in.*, 
       v_out.cosm_IO_vol,
       (v_in.cosm_volume * v_in.cosm_qty - v_out.cosm_IO_vol) 
           /  v_in.cosm_volume as  QtyStock,
       price.`cosmInOut_priceIn`
FROM (  SELECT C.cosm_name, 
               C.cosm_volume,
               SUM(IO.cosmInOut_qty) as cosm_qty 
        FROM cosmetics  C
        JOIN cosmInOut  IO
          ON C.`cosm_name` = IO.`cosmInOut_name`
        GROUP BY   C.cosm_name
     ) v_in
LEFT JOIN (SELECT cosmSpent_name,  SUM(cosmSpent_volume) as cosm_IO_vol
           FROM cosmSpent CS
           GROUP BY cosmSpent_name
          ) v_out
  ON v_in.`cosm_name` = v_out.`cosmSpent_name` 
LEFT JOIN ( SELECT  C.cosm_name, 
                    IF(@cosm_name = C.cosm_name, 
                       @volume,
                       0) minVolume,
                    @volume := IF(@cosm_name = C.cosm_name,
                                  @volume + C.cosm_volume * IO.cosmInOut_qty,
                                  IF(@cosm_name := C.cosm_name,
                                     C.cosm_volume,
                                     C.cosm_volume)
                                 ) as maxVolume,
                    `cosmInOut_priceIn`           
            FROM cosmetics  C
            JOIN cosmInOut  IO
              ON C.`cosm_name` = IO.`cosmInOut_name`
            CROSS JOIN (SELECT  @cosm_name := '', @volume := 0) Y
            ORDER BY C.cosm_volume, cosmInOut_id
           ) price
       ON v_in.cosm_name = price.cosm_name
      AND v_out.cosm_IO_vol BETWEEN price.minVolume AND price.maxVolume

输出

| cosm_name | cosm_volume | cosm_qty | cosm_IO_vol | QtyStock | cosmInOut_priceIn |
|-----------|-------------|----------|-------------|----------|-------------------|
|    L 5/66 |          60 |        3 |          80 |   1.6667 |                10 |