Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make SQL Select same row multiple times

Tags:

sql

select

I need to test my mail server. How can I make a Select statement that selects say ID=5469 a thousand times.

like image 841
Snoop Dogg Avatar asked Mar 18 '10 19:03

Snoop Dogg


People also ask

Can you use select twice in SQL?

Yes, you can use the same table twice by giving different aliases to the table. I think studying about self joins will help you understand.

Can we use select * with group by?

Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause. The original idea was to create the table in beginning of the query, so the (SELECT * FROM #TBL) could be used on the query itself, instead of defining the names on each GROUP BY.

How do you repeat a column in SQL?

To create a column with repeated data, set the mode of the column to REPEATED in the schema. A repeated field can be accessed as an ARRAY type in Google Standard SQL. A RECORD column can have REPEATED mode, which is represented as an array of STRUCT types.


1 Answers

In postgres there is a nice function called generate_series. So in postgreSQL it is as simple as:

select information from test_table, generate_series(1, 1000) where id = 5469

In this way, the query is executed 1000 times.

Example for postgreSQL:

CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; --To be able to use function uuid_generate_v4()

--Create a test table
create table test_table (
   id serial not null,
   uid UUID NOT NULL,
   CONSTRAINT uid_pk PRIMARY KEY(id));

-- Insert 10000 rows
insert into test_table (uid) 
select uuid_generate_v4() from generate_series(1, 10000);

-- Read the data from id=5469 one thousand times
select id, uid, uuid_generate_v4() from test_table, generate_series(1, 1000) where id = 5469;

As you can see in the result below, the data from uid is read 1000 times as confirmed by the generation of a new uuid at every new row.

 id  |uid                                   |uuid_generate_v4
 ----------------------------------------------------------------------------------------
 5469|"10791df5-ab72-43b6-b0a5-6b128518e5ee"|"5630cd0d-ee47-4d92-9ee3-b373ec04756f"
 5469|"10791df5-ab72-43b6-b0a5-6b128518e5ee"|"ed44b9cb-c57f-4a5b-ac9a-55bd57459c02"
 5469|"10791df5-ab72-43b6-b0a5-6b128518e5ee"|"3428b3e3-3bb2-4e41-b2ca-baa3243024d9"
 5469|"10791df5-ab72-43b6-b0a5-6b128518e5ee"|"7c8faf33-b30c-4bfa-96c8-1313a4f6ce7c"
 5469|"10791df5-ab72-43b6-b0a5-6b128518e5ee"|"b589fd8a-fec2-4971-95e1-283a31443d73"
 5469|"10791df5-ab72-43b6-b0a5-6b128518e5ee"|"8b9ab121-caa4-4015-83f5-0c2911a58640"
 5469|"10791df5-ab72-43b6-b0a5-6b128518e5ee"|"7ef63128-b17c-4188-8056-c99035e16c11"
 5469|"10791df5-ab72-43b6-b0a5-6b128518e5ee"|"5bdc7425-e14c-4c85-a25e-d99b27ae8b9f"
 5469|"10791df5-ab72-43b6-b0a5-6b128518e5ee"|"9bbd260b-8b83-4fa5-9104-6fc3495f68f3"
 5469|"10791df5-ab72-43b6-b0a5-6b128518e5ee"|"c1f759e1-c673-41ef-b009-51fed587353c"
 5469|"10791df5-ab72-43b6-b0a5-6b128518e5ee"|"4a70bf2b-ddf5-4c42-9789-5e48e2aec441"

Of course other DBs won't necessarily have the same function but it could be done:

See here.

like image 149
Krauss Avatar answered Oct 15 '22 03:10

Krauss