Problem with subquery

Asked

Viewed 73 times

0

Good night,

I need to bring the full subquery result:

(select (valor * 0.05) from tbl_exames) as 'Valor dos Exames',

In a delimiter:

delimiter //
create procedure comissao(idmedico int)
begin
    select nome_medico as 'Médico',
    (select (valor * 0.05) from tbl_exames) as 'Valor dos Exames',
    (select sum(valor * 0.05) from tbl_exames) as 'Valor Total das Comissões'
    from tbl_medico where id_medico = idmedico;
end //
delimiter ;

Where error: Error Code: 1242. Subquery Returns more than 1 Row.

It will list all the values of the tests being 5% of her.

Edit:

I’d like him to appear as follows:


Doctor - name


Examinations Value - value1, value2, value3, etc..


Total Commission Value - Sum of the above values


Edit2:

It would be so, but next to what I expected:

delimiter //
create procedure comissao(idmedico int)
begin
    select nome_medico as 'Médico',
    (select sum(valor * 0.05) from tbl_exames) as 'Valor Total das Comissões'
    from tbl_medico where id_medico = idmedico union
    select tipo as 'Exame', (valor * 0.05) as 'Valor da Comissão' from 
tbl_exames;
end //
delimiter ;
  • I made a change to the answer.

1 answer

1


He is saying that the subquery is returning more than 1 value, see its first subquery, it does not have the "sum" so it will return several values

select (valor * 0.05) from tbl_exames

Either you add the sum to it, or rethink your query.

This way should solve your problem:

delimiter //
create procedure comissao(idmedico int)
begin
    select nome_medico as 'Médico',
    (select sum(valor) from tbl_exames) as 'Valor dos Exames',
    (select sum(valor * 0.05) from tbl_exames) as 'Valor Total das Comissões'
    from tbl_medico where id_medico = idmedico;
end //
delimiter ;
  • I decided to change in Edit2, but I will accept your answer.

  • @Hermenicarette Guillotine when you find the solution to your problem yourself, you can and should answer. In this particular case, I believe that the solution of the Wictorchaves is equivalent to yours of the edition of the question, therefore in the current scenario I see no need to make a self answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.