我真的很伤心,希望得到一些帮助;-)
我有这个 MySQL 查询:
SELECT
sum(ot.value) AS total_value,
avg(ot.value) AS avg,
count(ot.value) AS count
FROM
orders_total ot,
orders o
WHERE
ot.orders_id = o.orders_id
AND ot.class = 'ot_total'
AND o.customers_name != 'Test Test'
AND o.date_purchased >= '2014-02-01T00:00:00'
AND o.date_purchased < '2014-03-01T00:00:00'
但我只希望在 orders_id 同时在 orders 和 orders_total 表中时选择这些值。
因此,如果orders_total 表中缺少orders_id,我不希望它包含在查询中。
我尝试了左右连接,如下所示,没有任何运气;-(
SELECT
sum(ot.value) AS total_value,
avg(ot.value) AS avg,
count(ot.value) AS count
FROM
orders o
RIGHT JOIN orders_total ot ON (ot.orders_id = o.orders_id)
WHERE
ot.orders_id = o.orders_id
AND ot.class = 'ot_total'
AND o.customers_name != 'Test Test'
AND o.date_purchased >= '2014-02-01T00:00:00'
AND o.date_purchased < '2014-03-01T00:00:00'
我究竟做错了什么?
提前致谢!巴斯