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 blog ( id INT PRIMARY KEY, title VARCHAR(255), content TEXT ); CREATE TABLE comments ( id INT PRIMARY KEY, blog_id INT, comment TEXT, FOREIGN KEY (blog_id) REFERENCES blog(id) ); INSERT INTO blog (id, title, content) VALUES (1, 'First Post', 'interesting stuff.'), (2, 'Second Post', 'interesting stuff!'), (3, 'Third Post', 'interesting stuff?'), (4, 'Fourth Post', 'more interesting stuff'), (5, 'Fifth Post', 'interesting more stuff'); INSERT INTO comments (id, blog_id, comment) VALUES (1, 1, 'Nice intro!'), (2, 1, 'Welcome!'), (3, 2, 'Good explanation'), (4, 3, 'Very useful'), (5, 3, 'I like this'), (6, 3, 'Thanks!'), (7, 5, 'Interesting'); SELECT b.*, COUNT(c.id) comment_count FROM blog b LEFT JOIN comments c ON c.blog_id = b.id GROUP BY b.id ORDER BY b.id DESC LIMIT 3;

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

Copy Clear