Skip to content

Instantly share code, notes, and snippets.

@arif98741
Created December 6, 2022 09:04
Show Gist options
  • Save arif98741/52850aca43acde53c6d5b9c80d0d21d0 to your computer and use it in GitHub Desktop.
Save arif98741/52850aca43acde53c6d5b9c80d0d21d0 to your computer and use it in GitHub Desktop.
Sql qUERY
/*CREATE TABLE posts (
id int NOT NULL,
title varchar(255) NOT NULL,
PRIMARY KEY (id)
);*/
/*CREATE TABLE view_table (
id int NOT NULL,
post_id int NOT null,
view_col int default 0
);*/
//query
select
p.*,
count(vt.id) as total
from
view_table vt
left join posts p on
p.id = vt.post_id
group by
vt.post_id
order by
total desc;
//data fetched
id|title |total|
--+-------+-----+
1|title 1| 2|
2|title 2| 2|
3|title 3| 1|
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment