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 product (id int, name varchar(64)); insert into product values (1, 'a1'), (2, 'a2'); create table history (prod_id int references a(id), date datetime, status int); insert into history values (1, '2022-01-01', 1), (1, '2022-01-02', 2), (1, '2022-01-03', 3), (2, '2022-01-01', 3), (2, '2022-01-02', 2); select * from product, lateral (select * from history where history.prod_id = product.id order by date desc limit 1) m; SELECT p.*, h.date, h.status FROM product p LEFT JOIN history h ON p.id = h.prod_id ORDER BY h.date;

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

Copy Clear