user2727704 user2727704 - 1 month ago 5
MySQL Question

How to write SQL query without a JOIN

I'm looking to convert this table:

NAME PROD IS_PAID
X A 0
X B 1
X C 0
..


To this, meaning if user paid for atleast one product then I would like to mark him as paid user for other products as well:

NAME PROD IS_PAID
X A 1
X B 1
X C 1
..


Is there a way to do that without doing any joins?

Thanks!

vkp vkp
Answer

You can use exists to do this.

select name,prod,
case when exists (select 1 from t where name=t1.name and is_paid=1) then 1 else 0 end is_paid
from t t1