我在我的数据库中有以下表格。我只列出了可用于连接的重要列。
MySQL连接表与COUNT值
目前我使用两个单独的查询每个COUNT值
对于分配的许可证
select
products.id,products.name,COUNT(assigned_licenses.id)
from
deployment_users
inner join
assigned_licenses
on
deployment_users.id = assigned_licenses.deployment_user_id
inner join
products
on
assigned_licenses.id = products.id
and
deployment_users.customer_id = 10
group by
assigned_licenses.id
;
对于许可证总数
select
products.id,products.name,COUNT(total_licenses.id)
from
customers
inner join
total_licenses
on
customers.iccode = licenses.iccode
inner join
products
on
total_licenses.id = products.id
and
customers.id = 10
group by
total_licenses.id
;
由于有比需要列出一个1000多的产品,我想将它们合并成一个单一query.How我能做到这一点?
很酷。你根本没有使用'deployment_users'。你能解释一下这个查询背后的想法吗? – Pradeep
嗯,思考嗯:) – chickahoona
如果你的意思是“它是如何工作”,诀窍是在这些“计数(独特...)”表达式与群组。如果你有5行的列X与值(5,2,4,2,1),那么它将返回4.对于值(3,3,5,5)它将返回2.所以它会检查如何存在许多“独特”的价值,并计数这些价值。这有助于你理解它吗? – chickahoona