我正在尝试计算每个条形码链接的唯一文件名。
我正在运行以下查询
select filename,barcode,item_code from images
join imports on import_id = imports.id
order by item_code desc, barcode;
我得到以下结果:
filename barcode item_code
5021523203124_1401260.jpg 5021523203124 9974
5021526203121_1390655.jpg 5021523203124 9974
5021526203121_1390655.jpg 5021523203124 9974
5021526203121_1390655.jpg 5021523203124 9974
我希望得到以下结果
filename img_count barcode item_code
5021523203124_1401260.jpg 1 5021523203124 9974
5021526203121_1390655.jpg 3 5021523203124 9974
答案 0 :(得分:0)
您可以按以下方式尝试-使用汇总和分组依据
select filename,count(*) as img_cnt,barcode,item_code
from images join imports on import_id = imports.id
group by filename,barcode,item_code
order by item_code desc, barcode
答案 1 :(得分:0)
与GROUP BY
聚合:
select
filename,
count(*) as img_count,
barcode,
item_code
from images
inner join imports
on import_id = imports.id
group by
filename,
barcode,
item_code
order by
item_code desc,
barcode;
请注意,理想情况下,您应该在查询中使用表别名,这将使我们能够确定每一列属于哪些表。
答案 2 :(得分:0)
您可以尝试
SELECT
filename , COUNT(*) as Img_Count, item_code, barcode
FROM images inner join imports on import_id = imports.id
order by item_code desc, barcode;
GROUP BY barcode,item_code,filename
答案 3 :(得分:0)
您的查询缺少group by
。但是,我强烈建议您学习使用合格的列名编写查询。按照书面规定,阅读查询的人不知道这些列来自何处。
我推测这些列来自imports
,但我不必猜测:
select imp.filename, count(*) as img_count, imp.barcode, imp.item_code
from imports imp
images i
on i.import_id = imp.id
group by imp.filename, imp.barcode, imp.item_code;