Sven Lorenzino
Sven Lorenzino

Reputation: 159

How to select the three highest outputs by select sum from other table?

in my current PHP project I would like to display the three most viewed posts on my website.

The number of views per post is stored in a different table, like the general data of the post. In this table an entry of the daily views is made for each day and each post.

Currently I can get all the data from the general post table,

$query = "SELECT * FROM `articles` WHERE `article_category_id` = '1' DESC LIMIT 3";
$most_viewed_post_query = mysqli_query($connection, $query);

    while($row = mysqli_fetch_assoc($most_viewed_post_query)) {

        $article_id = $row['article_id'];
        $article_categorie = $row['article_category_id'];
        $article_title = $row['article_title']; 
        $article_author = $row['article_author'];
        $article_date = $row['article_date'];
CREATE TABLE `articles` (
  `article_id` int(3) NOT NULL,
  `article_category_id` int(3) NOT NULL,
  `article_title` varchar(255) COLLATE utf8mb4_german2_ci NOT NULL,
  `article_author` varchar(255) COLLATE utf8mb4_german2_ci NOT NULL,
  `article_date` date NOT NULL,
  `article_content` text COLLATE utf8mb4_german2_ci NOT NULL,
  `article_tag` varchar(255) COLLATE utf8mb4_german2_ci NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_german2_ci;

and from the views table I can only get the sum of one post by ID.

$query = "SELECT SUM(day_views_count) AS article_views FROM days_views WHERE view_id = $article_id";
$get_article_views = mysqli_query($connection, $query);

    $row = mysqli_fetch_array($get_article_views);
    $article_views = $row['article_views'];
CREATE TABLE `days_views` (
  `view_id` int(11) NOT NULL,
  `day` date NOT NULL,
  `day_views_count` int(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_german2_ci;

I have no idea how to write the two SELECTS together to make the output fit.

Upvotes: 1

Views: 57

Answers (3)

Scenus
Scenus

Reputation: 147

You can use something like this:

SELECT SUM(day_views_count) AS article_views 
FROM days_views 
WHERE view_id = $article_id
ORDER BY article_views LIMIT 3

Upvotes: 2

Werzaire
Werzaire

Reputation: 78

How about:

select a.*
from articles a
inner join (
    select view_id 
    from article_views 
    group by view_id
    order by day_views_count desc
    limit 3
) v on v.view_id = a.article_id

The other suggestion where sum(day_views_count) is used, will sum the entire column for each entry thus it won't sort properly. My suggestion orders it by the count, then takes top three. Does it work?

Upvotes: 0

GMB
GMB

Reputation: 222422

You could join table articles with an aggregatd query that pulls out the of the three most viewed view_ids from article_views, like:

select a.*
from articles a
inner join (
    select view_id 
    from article_views 
    group by view_id
    order by sum(day_views_count) desc
    limit 3
) v on v.view_id = a.azrticle_id

Upvotes: 0

Related Questions