Created
December 6, 2022 09:04
-
-
Save arif98741/52850aca43acde53c6d5b9c80d0d21d0 to your computer and use it in GitHub Desktop.
Sql qUERY
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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