MySQL脚本触发错误#1241

时间:2018-04-02 09:30:14

标签: php mysql sql mysqli

我有下面的脚本,它应该为已提供的一系列ID提供价格。

但它需要1ID的价格,而棘手的部分是,我希望能够有预定的价格更新。

这意味着它需要采用<= UTC_TIMESTAMP的价格小于或等于当前时间。

SELECT 
`product_pricing`.`wo_id`,
`product_pricing`.`currency` AS price2_currency,
`product_pricing`.`price` AS price2,
`product_pricing`.`formula_id`,
`product_pricing`.`vat_calculated` AS price2_vat_calculated,
`product_pricing`.`vat_id`,
`product_pricing`.`timestamp_valid`,

`product_price_formulas`.`formula_id`,
`product_price_formulas`.`formula` price2_formula

FROM 
`product_pricing`

LEFT JOIN `product_price_formulas` 
ON `product_pricing`.`formula_id` = `product_price_formulas`.`formula_id`

WHERE 
`product_pricing`.`wo_id` IN (

    SELECT 
    `product_pricing`.`wo_id`,
    `product_pricing`.`timestamp_valid`

    FROM `product_pricing`

    WHERE 
    `product_pricing`.`wo_id` 
        IN ('015724', '028791', '015712', '015715', '015717', '039750', '028791')
    AND `product_pricing`.`timestamp_valid` <= UTC_TIMESTAMP

    ORDER BY `product_pricing`.`timestamp_valid` DESC        
 )

这可能吗?

样本数据:当前输出

——————————————————————————————————————————————————————————————————
 | wo_id    | price2    | timestamp_valid 
——————————————————————————————————————————————————————————————————
 | 028791   | 8000      | 2018-03-20 19:55:41
 | 028791   | 6000      | 2018-04-01 19:55:41
 | 028791   | 4000      | 2018-04-20 19:55:41
 | 015724   | 3000      | 2018-04-18 19:55:41
 | 015724   | 1500      | 2018-03-01 19:55:41
 ....

通缉输出:

——————————————————————————————————————————————————————————————————
 | wo_id    | price2    | timestamp_valid 
——————————————————————————————————————————————————————————————————
 | 028791   | 6000      | 2018-04-01 19:55:41
 | 015724   | 1500      | 2018-03-01 19:55:41

3 个答案:

答案 0 :(得分:1)

我猜你的问题出在IN条款上。

您在IN子句中选择了两个字段。

修改

您需要按wo_id和最大timestamp_valid自行加入子查询。

SELECT 
    `product_pricing`.`wo_id`,
    `product_pricing`.`currency` AS price2_currency,
    `product_pricing`.`price` AS price2,
    `product_pricing`.`formula_id`,
    `product_pricing`.`vat_calculated` AS price2_vat_calculated,
    `product_pricing`.`vat_id`,
    `product_pricing`.`timestamp_valid`,
    `product_price_formulas`.`formula_id`,
    `product_price_formulas`.`formula` price2_formula
FROM 
`product_pricing`
LEFT JOIN `product_price_formulas` ON `product_pricing`.`formula_id` = `product_price_formulas`.`formula_id`
INNER JOIN 
(
    SELECT 
        `product_pricing`.`wo_id`,
        MAX(`timestamp_valid`) AS MaxDate
    FROM `product_pricing`
    WHERE 
        `product_pricing`.`timestamp_valid` <= UTC_TIMESTAMP
    GROUP BY 
        `product_pricing`.`wo_id`
)as temp ON temp.wo_id = `product_pricing`.`wo_id` AND temp.MaxDate = `product_pricing`.`timestamp_valid`
WHERE  
    `product_pricing`.`wo_id` IN ('015724', '028791', '015712', '015715', '015717', '039750', '028791')

答案 1 :(得分:0)

我想你想要这样的东西:

SELECT pp.*, ppf.formula_id, ppf.formula as price2_formula
FROM product_pricing pp LEFT JOIN
     product_price_formulas ppf
     ON pp.formula_id = ppf.formula_id    
WHERE (pp.wo_id, pp.timestamp_valid) IN
          (SELECT pp2.wo_id, MAX(pp2.timestamp_valid)
           FROM product_pricing pp2
           WHERE pp2.wo_id IN ('015724', '028791', '015712', '015715', '015717', '039750', '028791') AND
                pp2.timestamp_valid <= UTC_TIMESTAMP
          );

ORDER BY在子查询中毫无意义,所以这是我对你想要的最好的猜测。

我将此结果与您使用IN的结构相提并论,但我会使用相关的子查询和=

WHERE pp.timestamp_valid = (SELECT MAX(pp2.timestamp_valid)
                            FROM product_pricing pp2
                            WHERE pp2.wo_id = pp.wo_id AND
                                  pp2.timestamp_valid <= UTC_TIMESTAMP
                           ) AND
      pp2.wo_id IN ('015724', '028791', '015712', '015715', '015717', '039750', '028791');

答案 2 :(得分:0)

您可以使用最新的valid_timestamp记录的派生结果集来加入product_pricing。

如果我们暂时忽略product_pricing_formula表(因为您没有将其包含在您的样本数据和结果中),这将给出。

SELECT 
p.`wo_id`,
p.`price` AS price2,
p.`timestamp_valid`
FROM `product_pricing` p
JOIN (SELECT wo_id, MAX(timestamp_valid) AS max_valid_ts
      FROM `product_pricing`
      WHERE `timestamp_valid` <= UTC_TIMESTAMP
      GROUP BY wo_id) d
ON (d.wo_id = p.wo_id AND d.max_valid_ts = p.timestamp_valid)
WHERE p.`wo_id` IN (015724, 028791);

Sqlfiddle

上试用