Home > database >  Is it possible to combine the results of multiple SEPARATED select statements?
Is it possible to combine the results of multiple SEPARATED select statements?

Time:06-30

I'm new to SQL in general, I don't know if this is already answered or just common knowledge, but I've failed to find similar question.

Is it possible to append multiple select statements when there's other processes between it? For example,

set @a = 1;
#first select
select @a as alphabet;

#random code
set @a = 2;
insert into `dummy`(`iddummy`) values @a;

#second select
select @a as alphabet;

and get results like

alphabet
--------
1
2

I want appended result like that because I'm working with for my front end program, using select to return error message or insert results (for review). This becomes a problem when one of my stored procedure calls other similar procedure, resulting in multiple select results with just one call

CodePudding user response:

Save the results of the first query in a temporary table, then select from it in your final query:

set @a = 1;
create temporary table query1
select @a as alphabet;
set @a = 2;
insert into dummy(iddummy) values (@a);
select alphabet from query1
union all
select @a;

Produces:

alphabet
1
2

See live demo (also showing dummy was inserted into).

  • Related