in reply to Re: Perl and MySQL: Get number of rows affected
in thread Perl and MySQL: Get number of rows affected

Wrong. It will return the number of not-NULL value's in the rows retrieved, which is always less or equal to the number of rows actually fetched!

The number of rows fetched is returned by COUNT(*), i.e.

SELECT COUNT(*) FROM table WHERE distinguishing_value = ?

See MySQL grouping fuctions manual for more details.

Replies are listed 'Best First'.
Re^3: Perl and MySQL: Get number of rows affected
by Samy_rio (Vicar) on Jul 15, 2006 at 12:07 UTC

    In MySql documentation, I got the below query:

    Query from documentation : SELECT COUNT(ip), AVG(down) from test;

    When I run the below query, i am getting the following output.

    Query which I ran: mysql> select count(us_name) from table where us_name='samy'; +----------------+ | count(us_name) | +----------------+ | 6 | +----------------+ 1 row in set (0.00 sec)

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

      It seems that you don't know what the NULL values are :) Yes, in the case when the column value is declared as NOT NULL, COUNT(value) is equivalent to COUNT(*).

      But look at the example:

      CREATE TEMPORARY TABLE testnull0 ( id INT NOT NULL auto_increment, value VARCHAR(100), PRIMARY KEY(id) ) ENGINE=MyISAM; INSERT INTO testnull0 (id, value) VALUES (1, 'hello'), (2, 'googbye'), + (3, NULL); SELECT COUNT(id), COUNT(value), COUNT(*) FROM testnull0;
      Being processed by MySQL (you must specify your DB name, of course), it prints:
      +-----------+--------------+----------+ | COUNT(id) | COUNT(value) | COUNT(*) | +-----------+--------------+----------+ | 3 | 2 | 3 | +-----------+--------------+----------+
      Got the idea? You cannot rely on the suggestion that the value column is declared as NOT NULL, if you did not create the table yorself :) while COUNT(*) always works as expected.

      It's REALLY off-topic here, but i think that answering an OT question wrong is worse than answering it right—and even worse than not answering it at all.