带连接的SQL查询仅选择1行

时间:2014-05-02 13:50:34

标签: mysql sql

我的SQL出现问题;查询只选择一行,即使它应该选择更多的行。

选择语句

SELECT p.id,
       p.product_name,
       p.product_date,
       coalesce(t.unique_hits, 0) AS unique_hits,
       coalesce(t.total_hits, 0) AS total_hits,
       coalesce(COUNT(s.product_id), 0) AS total_sales,
       coalesce(SUM(s.amount), 0) AS total_revenue
FROM products p
LEFT OUTER JOIN tracking_hits t ON p.id = t.product_id
LEFT OUTER JOIN transactions s ON p.id = s.product_id
WHERE p.vendor_id = 10;

DDL示例:
http://sqlfiddle.com/#!2/b393df/3

我添加了两个具有相同供应商ID的产品,当我运行查询时,它只选择一个产品,即使它应该同时选择它们。

用于创建表和数据的SQL

CREATE TABLE products(id int, product_name varchar(100)
      , product_date varchar(100), vendor_id int);

CREATE TABLE tracking_hits(id int, product_id int, unique_hits int
      , total_hits int);

CREATE TABLE transactions(id int, product_id int, amount double);

插入一些数据

INSERT INTO products(id, product_name, product_date, vendor_id)
VALUES(0, "Product", "2014-05-02", 10);

INSERT INTO products(id, product_name, product_date, vendor_id)
VALUES(1, "Product", "2014-05-02", 10);

INSERT INTO tracking_hits(id, product_id, unique_hits, total_hits)
VALUES(0, 0, 10, 52);

INSERT INTO tracking_hits(id, product_id, unique_hits, total_hits)
VALUES(1, 1, 52, 124);    

INSERT INTO transactions(id, product_id, amount)
VALUES(0, 0, 19.00);

INSERT INTO transactions(id, product_id, amount)
VALUES(1, 0, 19.00);

INSERT INTO transactions(id, product_id, amount)
VALUES(2, 0, 19.00);

INSERT INTO transactions(id, product_id, amount)
VALUES(3, 1, 29.00);

这里的问题是什么?

3 个答案:

答案 0 :(得分:1)

您在执行聚合函数sum()

时缺少分组
SELECT p.id,
       p.product_name,
       p.product_date,
       coalesce(t.unique_hits, 0) AS unique_hits,
       coalesce(t.total_hits, 0) AS total_hits,
       coalesce(COUNT(s.product_id), 0) AS total_sales,
       coalesce(SUM(s.amount), 0) AS total_revenue
FROM products p
LEFT OUTER JOIN tracking_hits t ON p.id = t.product_id
LEFT OUTER JOIN transactions s ON p.id = s.product_id
WHERE p.vendor_id = 10
group by p.id
;

答案 1 :(得分:1)

以下是您的查询:

SELECT p.id,
       p.product_name,
       p.product_date,
       coalesce(t.unique_hits, 0) AS unique_hits,
       coalesce(t.total_hits, 0) AS total_hits,
       coalesce(COUNT(s.product_id), 0) AS total_sales,
       coalesce(SUM(s.amount), 0) AS total_revenue
FROM products p
LEFT OUTER JOIN tracking_hits t ON p.id = t.product_id
LEFT OUTER JOIN transactions s ON p.id = s.product_id
WHERE p.vendor_id = 10;

这是一个没有group by的聚合查询(因为在sum()中使用了聚合函数count()select)。这样的查询总是返回一行。我想你想要:

SELECT p.id,
       p.product_name,
       p.product_date,
       coalesce(t.unique_hits, 0) AS unique_hits,
       coalesce(t.total_hits, 0) AS total_hits,
       coalesce(COUNT(s.product_id), 0) AS total_sales,
       coalesce(SUM(s.amount), 0) AS total_revenue
FROM products p
LEFT OUTER JOIN tracking_hits t ON p.id = t.product_id
LEFT OUTER JOIN transactions s ON p.id = s.product_id
WHERE p.vendor_id = 10
GROUP BY p.id, t.unique_hits, t.total_hits;

答案 2 :(得分:1)

基本问题是在使用GROUP BYSUM等聚合时缺少COUNT。除此之外,我认为你可以通过避免所有coalesce函数来简化你的查询。 DBMS应该足够聪明,可以将null值作为0处理,而无需明确告知它。如果你想在0之间拥有不同的价值,情况会发生变化。这是一个工作(sqlfiddle)示例:

SELECT p.id,
       p.product_name,
       p.product_date,
       t.unique_hits AS unique_hits,
       t.total_hits AS total_hits,
       COUNT(s.product_id) AS total_sales,
       SUM(s.amount) AS total_revenue
FROM products p
LEFT JOIN tracking_hits t ON p.id = t.product_id
LEFT JOIN transactions s ON p.id = s.product_id
WHERE p.vendor_id = 10
GROUP BY p.id, p.product_name, p.product_date, t.unique_hits, t.total_hits; 

更新:我也t.unique_hits, t.total_hits添加了GROUP BY