Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize | PHPize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
Copy Format Clear
create table statuses (id int primary key, title text); insert into statuses values (1, 'Completed'), (2, 'Not Completed'); create table problems (id int primary key, title text); insert into problems values (1, 'Problem 1'), (2, 'Problem2'), (3, 'Problem3'); create table tasks (id int, problem_id int references problems, status_id int references statuses); insert into tasks values (1, 1, 1), (2, 1, 2); select problems.*, case when coalesce(max(status_id), 2) = 2 then 'Not Completed' else 'Completed' end problem_status from problems left join tasks on tasks.problem_id = problems.id group by problems.id, problems.title order by problems.id;

Stuck with a problem? Got Error? Ask AI support!

Copy Clear