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 main ( id int AUTO_INCREMENT PRIMARY KEY, id_tab2 int, type enum('mod1', 'mod2') ); insert into main values (, 44, 'mod1'), (, 75, 'mod2'), (, 66, 'mod1'); create table tab1 ( id int, title varchar(32) ); insert into tab1 values (44, 'title1'), (66, 'title2'); create table tab2 ( id int, title varchar(32) ); insert into tab2 values (75, 'title6'), (88, 'title5'); select main.*, coalesce(tab1.title, tab2.title) as title from main left join tab1 on tab1.id = main.id_tab2 and main.type = 'mod1' left join tab2 on tab2.id = main.id_tab2 and main.type = 'mod2' order by id;

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

Copy Clear