SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
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 ChatGPT!
Copy Clear