简体   繁体   English

如何计算具有不同记录的查询?

[英]How to count query with distinct records?

I have a query with DISTINCT columns.我有一个带有 DISTINCT 列的查询。

SELECT 
    DISTINCT column_1
    column_2,
    column_3,
    column_4,
    column_5,
    column_6,
    column_7,
    column_8,
    column_9
FROM 
    my_table 
WHERE 
    column_4 IN(some array) 
    and ... 
    order by column_1, column_2

This query is correct which returns some 1000 records.这个查询是正确的,它返回了大约 1000 条记录。 Now I need to find out total number of records using count.现在我需要使用计数找出记录总数。

I have tried select count(DISTINCT column_1) but the count record is not 1000.我试过 select count(DISTINCT column_1) 但计数记录不是 1000。

I can apply count the records from PHP but I am looking to do it from query itself.我可以从 PHP 应用计数记录,但我希望通过查询本身来完成。

Can anybody please teach me how to write count query?有人可以教我如何编写计数查询吗?

COUNT(DISTINCT column_1) only counts the number of distinct column_1 values. COUNT(DISTINCT column_1)只计算不同column_1值的数量。 To get the number of results that are in your query, you need to apply COUNT(DISTINCT...) to all the columns:要获得查询中的结果数,您需要将COUNT(DISTINCT...)应用于所有列:

SELECT COUNT(DISTINCT column_1, column_2, column_3, column_4, ... column_9)
FROM my_table 
WHERE ...

Note that this is a MySQL extension to SQL and will not work on other DBMS.请注意,这是 SQL 的 MySQL 扩展,不适用于其他 DBMS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM