Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL, counting from 0 to 10

Tags:

sql

mysql

it may sound stupid, but i want to know how (if possible) to count from 0 to 10 by a sql command. i mean i want a sql query which produces one column consisting of numbers from 0 to 10, like this:

0
1
2
3
4
5
6
7
8
9
10

i'm currently using MySQL 5.0

like image 295
ali Avatar asked Jul 03 '10 07:07

ali


People also ask

What is COUNT 1 in SQL with example?

1) COUNT(*) When * is used as an argument, it simply counts the total number of rows including the NULLs. In the example, we will get 11 as count as we have 11 rows in table.

How do I print 1 to 10 numbers in SQL?

This query: select printnum(1,10) from dual; ========i used function for printing 1-10 in form of(1,2,3,4.. 10).

Does COUNT in SQL COUNT 0?

The SQL COUNT() function returns the number of rows in a table satisfying the criteria specified in the WHERE clause. It sets the number of rows or non NULL column values. COUNT() returns 0 if there were no matching rows.


2 Answers

If you simply want the exact values 0 through 10, then the following will work in all DBMS:

Select Num
From    (
        Select 0 As Num
        Union All Select 1
        Union All Select 2
        Union All Select 3
        Union All Select 4
        Union All Select 5
        Union All Select 6
        Union All Select 7
        Union All Select 8          
        Union All Select 9
        Union All Select 10
        ) As Numbers

That said, the simplest way to solve problems where you need a sequential list of integers is to create a permanent Numbers or Tally table that contains this list. In this way, it will not matter how it was populated as it need only be populated once.

like image 90
Thomas Avatar answered Sep 25 '22 00:09

Thomas


select number  as n
from master..spt_values 
where type='p' 
and number between 0 and 10

Sorry I just noticed you were in MYSQL, this is MSSQL Only.

like image 40
SPE109 Avatar answered Sep 26 '22 00:09

SPE109