By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
select version();
version() |
---|
8.0.27 |
create table U(id int , nm char(11));
create table P(u_id int, txt varchar(64));
insert into U(id,nm) values (-1,'Anonymus'), (1,'One'),(2,'Two'),
(3,'Three'),(4,'Four');
insert into P(u_id, txt) values (1,'ntrcn11'),(1,'lkjhlkj456'),(3,'lkjhlkj456'),
(33,'Comment23'),(44,'Qury12');
select * from U; select * from P;
id | nm |
---|---|
-1 | Anonymus |
1 | One |
2 | Two |
3 | Three |
4 | Four |
u_id | txt |
---|---|
1 | ntrcn11 |
1 | lkjhlkj456 |
3 | lkjhlkj456 |
33 | Comment23 |
44 | Qury12 |
select IFNULL(u1.nm,u2.nm), p.txt
from P p
left join U u1 on p.u_id = u1.id
inner join U u2 on u2.id=-1
IFNULL(u1.nm,u2.nm) | txt |
---|---|
One | ntrcn11 |
One | lkjhlkj456 |
Three | lkjhlkj456 |
Anonymus | Comment23 |
Anonymus | Qury12 |