Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a row generator in MySQL?

Is there a way to generate an arbitrary number of rows that can be used in a JOIN similar to the Oracle syntax:

SELECT LEVEL FROM DUAL CONNECT BY LEVEL<=10 
like image 671
GameFreak Avatar asked Mar 31 '09 15:03

GameFreak


People also ask

How do I combine multiple rows into one in MySQL?

MySQL | Group_CONCAT() Function. The GROUP_CONCAT() function in MySQL is used to concatenate data from multiple rows into one field. This is an aggregate (GROUP BY) function which returns a String value, if the group contains at least one non-NULL value. Otherwise, it returns NULL.

How do I add more rows in MySQL?

When inserting a single row into the MySQL table, the syntax is as follows: INSERT INTO table_name(column_1,column_2,column_3) VALUES (value_1,value_2,value_3); In the INSERT INTO query, you should specify the following information: table_name : A MySQL table to which you want to add a new row.

How do I row a column in MySQL?

A database table can store different types of data and sometimes we need to transform row-level data into column-level data. This problem can be solved by using the PIVOT() function. This function is used to rotate rows of a table into column values.


2 Answers

Hate to say this, but MySQL is the only RDBMS of the big four that doesn't have this feature.

In Oracle:

SELECT  * FROM    dual CONNECT BY         level < n 

In MS SQL (up to 100 rows):

WITH hier(row) AS         (         SELECT  1         UNION ALL         SELECT  row + 1         FROM    hier         WHERE   row < n         ) SELECT  * FROM    hier 

or using hint up to 32768

WITH hier(row) AS         (         SELECT  1         UNION ALL         SELECT  row + 1         FROM    hier         WHERE   row < 32768         ) SELECT  * FROM    hier OPTION (MAXRECURSION 32767) -- 32767 is the maximum value of the hint 

In PostgreSQL:

SELECT  * FROM    generate_series (1, n) 

In MySQL, nothing.

like image 190
Quassnoi Avatar answered Sep 29 '22 02:09

Quassnoi


In MySql, it is my understand that you can get more than one row with a SELECT with no table (or DUAL).

Therefore, to get multiple rows, you do need a real or temporary table with at least the required number of rows.

However, you do not need to build a temporary table as you can use ANY existing table which has at least the number of rows required. So, if you have a table with at least the required number of rows, use:

SELECT  @curRow := @curRow + 1 AS row_number FROM    sometable  JOIN    (SELECT @curRow := 0) r WHERE   @curRow<100; 

Just replace "sometable" with the name of any table of yours with at least the required number of rows.

PS: The "r" is a table "alias": I could have used "AS r". Any subquery in a FROM or JOIN clause creates a "derived table" which, as with all tables, must have a name or alias. (See MySql manual: 13.2.9.8. Subqueries in the FROM Clause)

like image 33
DrDave Avatar answered Sep 29 '22 03:09

DrDave