这是我的第一个查询
SELECT
tax_productid,
CONCAT(GROUP_CONCAT(tax_code SEPARATOR " "), " ", sum(tax_iva), "%") AS tax
FROM tb_tax t group by tax_productid order by tax_productid, prd_type desc;
返回适用于产品的税(多种税种可适用于单品)
输出数据,如。
tax_productid tax
=========================
1 tax1 tax2 10%
2 tax1 tax2 20%
3 tax1 tax2 30%
现在有另一个表product
,查询就像
SELECT * FROM product ORDER BY productName;
返回10条产品记录。
我想获得产品明智的税单,但如果没有税收显示为空
SELECT
t1.*,
CONCAT(GROUP_CONCAT(tax_code SEPARATOR " "), " ", sum(tax_iva), "%") AS tax
FROM
product t1, tb_tax t2
WHERE
t2.tax_product = t1.id;
此查询仅返回3行。如果表tb_tax
没有产品条目然后返回null,我想列出所有产品。所以我怎么能实现这个目标。
答案 0 :(得分:1)
您的查询正在使用INNER JOIN
,在这种情况下您应该使用LEFT JOIN
:
SELECT
t1.*,
t2.tax
FROM
product t1
LEFT JOIN (
SELECT
tax_productid,
CONCAT(GROUP_CONCAT(tax_code SEPARATOR " "), " ", sum(tax_iva), "%") AS tax
FROM tb_tax
GROUP BY tax_productid
) t2 ON t2.tax_productid = t1.id
答案 1 :(得分:0)
使用LEFT JOIN
。
SELECT
t1.*,
CONCAT(GROUP_CONCAT(tax_code SEPARATOR " "), " ", sum(tax_iva), "%") AS tax
FROM
product t1
LEFT JOIN
tb_tax t2
ON
t2.tax_product = t1.id;