在左连接中使用变量

时间:2016-04-06 09:27:41

标签: php mysql

我有以下查询:

 SELECT
  o.id            AS id,
  o.price_decimal AS price_decimal,
  p.sum_with_tax  AS sum_withtax,
  @price_decimal := price_decimal
FROM orders o
  LEFT JOIN (
              SELECT order_id,quantity,tax_rate,price,
                ROUND(SUM((price * (tax_rate / 100 + 1) * quantity)), @price_decimal) AS sum_with_tax,
                sum(quantity * price)                                                 AS sum,
                sum(quantity)                                                         AS sum_quantity
              FROM orders_positions
              GROUP BY order_id) p
    ON o.id = p.order_id

我需要 @price_decimal 变量。该字段的内容在表格订单中定义。

1 个答案:

答案 0 :(得分:0)

请尝试一下:

SELECT 
    o.id AS id, o.price_decimal AS price_decimal, p.sum_with_tax AS sum_withtax,
p.order_id,  p.quantity,     p.tax_rate,     p.price,
      ROUND(SUM((p.price * (p.tax_rate / 100 + 1) * p.quantity)), o.price_decimal) AS sum_with_tax,
            sum(p.quantity * price) AS sum,
            sum(p.quantity) AS sum_quantity
FROM  orders o
inner join orders_positions p ON o.id = p.order_id
group by p.order_id