我有以下数据库架构:
CREATE SCHEMA products;
CREATE TABLE products.product_group (
id bigserial NOT NULL PRIMARY KEY,
name varchar(255) NOT NULL
)
CREATE TABLE products.product (
id bigserial NOT NULL PRIMARY KEY,
name varchar(255) NOT NULL,
product_group_id bigint REFERENCES products.product_group(id),
color varchar(255) NOT NULL
)
INSERT INTO products.product_group(name) values ('product group 1');
INSERT INTO products.product(name,product_group_id,color) VALUES ('product 1',1,'RED');
INSERT INTO products.product(name,product_group_id,color) VALUES ('product 1',1,'BLUE');
INSERT INTO products.product(name,product_group_id,color) VALUES ('product 1',1,'GREEN');
如果我执行以下查询:
SELECT count(*), pg.name FROM products.product p
LEFT JOIN products.product_group pg ON (p.product_group_id=pg.id)
WHERE pg.id = 1
AND p.color = 'RED'
GROUP BY pg.name
我得到结果:
1;"product group 1"
如果我执行以下查询:
SELECT count(*), pg.name FROM products.product p
LEFT JOIN products.product_group pg ON (p.product_group_id=pg.id)
WHERE pg.id = 1
AND p.color = 'YELLOW'
GROUP BY pg.name
我没有得到任何结果,但我想要:
0;"product group 1"
我需要如何更改查询以获得我想要的结果?