how to know how many records a query returns sql server 2012 with php

2

I have the following query SELECT * FROM REPORTE and I want to know how I do to count how many records are in the table, in SQL Server 2012. With php I can not find how to perform certain functions based on the number of records.

    
asked by pedro 23.08.2018 в 00:48
source

1 answer

2

It does not depend on PHP what you want to do, it is an operation in SQL language that you must execute.

The form is using an aggregation function, which in this case is COUNT() which can be indicated in these ways

  • COUNT(*) Will count all the records in the table, considers that the use of the wildcard * will count even those values NULL or duplicados

  • COUNT(id) Using to count the records in the table, leaning on the id column

  • YOUR SQL SHOULD BE SATISFIED

    SELECT COUNT(*) FROM TablaName;
    

    For example, to indicate you as a result

    //20 si es que tuvieras 20 registros hechos
    

    I'll give you the following example

    We have the following table with user records, some of them repeated

    CREATE TABLE usuarios(
       name VARCHAR(100),
       email VARCHAR(100)
    );
    

    These are the records of the tables

    INSERT INTO usuarios
    VALUES
    ('alfred', '[email protected]'),
    ('beto', '[email protected]'),
    ('daniel', '[email protected]'),
    ('pedro', '[email protected]'),
    ('beto', '[email protected]'),
    ('daniela', '[email protected]');
    

    If I do a COUNT(*) I get the following

    SELECT COUNT(*) FROM usuarios;
    //retorna 6
    

    If, on the contrary, I now want the count but only the unique values, I must indicate instead of the wildcard operator * , the name of the column and the reserved word DISTINCT

    SELECT COUNT(DISTINCT name) FROM usuarios;
    //retorna 5
    
        
    answered by 23.08.2018 в 00:55