Error Code: 1241. Operand should contain 1 column(s) when trying a subquery

Viewed 113

I have created two table person with column id, name, mobile and another is transaction table with id, quantity, rate, total, pay, due, one person can have multiple entry in transaction table. Now, I want to see the total quantity, total payment and total due against each name in person table. I don't know much about sql, that's why I try for total quantity first but it give me error: "Error Code: 1241. Operand should contain 1 column(s)"

My sql is:

 select id,name from person where(id) in 

(select id, sum(quantity) from transaction group by id);

What can i do for, showing all field like: total quantity, total pay, total due.

1 Answers

You can use join

select id,name,total
from person p join
(select id, sum(quantity) as total from transaction group by id) as t on p.id=t.id

OR

select p.id,name,sum(quantity) as total from person p join transaction t 
on p.id=t.id
group by p.id, name
Related