Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does MySQL count from 1 and not 0?

The first element of arrays (in most programming languages) has an id (index) of 0. The first element (row) of MySQL tables has an (auto incremented) id of 1. The latter seems to be the exception.

like image 498
Emanuil Rusev Avatar asked Feb 24 '13 18:02

Emanuil Rusev


People also ask

Does MySQL count from 0 or 1?

The first element of arrays (in most programming languages) has an id (index) of 0. The first element (row) of MySQL tables has an (auto incremented) id of 1.

Does count in SQL start from 0?

Counting in SQL generally starts as "1". For instance, the substring operations count characters in a string from 1 (and not 0). row_number() enumerates rows in a group, starting from 1 (and not 0).

How does count works in MySQL?

The COUNT(*) function returns the number of rows in a dataset using the SELECT statement. The function counts rows with NULL, duplicate, and non-NULL values. You can also use the WHERE clause to specify a condition.

How do I count records in MySQL?

MySQL COUNT() Function The COUNT() function returns the number of records returned by a select query.


1 Answers

The better question to ask is "why are arrays zero-indexed?" The reason has to do with pointer arithmetic. The index of an array is an offset relative to the pointer address. In C++, given array char x[5], the expressions x[1] and *(x + 1) are equivalent, given that sizeof(char) == 1.

So auto increment fields starting at 1 make sense. There is no real correlation between arrays and these fields.

like image 67
pyrospade Avatar answered Sep 22 '22 16:09

pyrospade