我不是 DBA,但仍在尝试找出如何解决以下问题。 我只从数据库中检索相应类别动物的计数。 现在,在通过多个连接从该查询中检索计数后,我得到了所有计数(在总计数列中提到) 但我想用另外两列表示类型和描述来表示该数据。但我不知道该怎么做。 就像,我从具有连接的查询中获取总计数,我想将这些计数显示为
Type_of_animal|description |total_count
------------- |---------------------------------------|-----------
Reptiles |Reptiles are cold-blooded vertebrates | 5000
Mammals |People are mammals | 80000
Birds |Any which have feathers | 1000000
我想通过手动命名 type_of_animal 及其描述来表示这些计数,以针对每个计数。
一个查询是
SELECT count(catmang.category_id) AS 'Total Counts'
FROM category catmang
JOIN category_identifier catident ON catident.category_id = catmang.category_id
WHERE category_id = 5
最佳答案
你的意思是:
SELECT category_identifier.*, COUNT(*) as total_count
FROM category_identifier
LEFT JOIN category using(category_id)
GROUP BY category_identifier.category_id
(可能用category_identifier.*交换为.type和.description)
更新
由于原始发布者不想使用数据库中实际存在的数据(剥夺数据库中数据的所有好处),因此可以创建一个“虚拟”表,但它非常冗长且烦人。添加此连接:
LEFT JOIN (
SELECT 'Reptiles' as Type_of_animal,
'Reptiles are cold-blooded vertebrates' as description,
1 as category_id
UNION
SELECT 'Mammals' as Type_of_animal,
'Reptiles are cold-blooded vertebrates' as description,
2 as category_id
UNION
.......
) as texts USING(category_id)
并在选择中使用texts.type_of_animal、texts.description。你可能必须修改category_ids...
关于mysql - 如何通过手动表示每行来表示查询中的数据计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45983119/