tisuchi
tisuchi

Reputation: 129

PHP Fetch Data from MySql Table like Twitter Trend

I have a mysql table as follows-

crId    ccId    ccatId  entryDate
458     186     12      2016-01-01
459     186     37      2016-01-01
460     186     117     2016-01-01
461     187     12      2016-01-04
462     187     117     2016-01-05
463     187     117     2016-01-07
464     187     12      2016-01-07

Now, I want to fetch data from this table like twitter trend part.

How can I make this? I cannot find out the solution? Can anybody please help me?

Expected Outcomes-

12
3 times

117
3 times

12
2 times

Upvotes: 1

Views: 152

Answers (2)

Ashish Detroja
Ashish Detroja

Reputation: 1082

Plese check below query :

SELECT ccatId, count( ccatId ) AS count FROM `tablename` WHERE `entryDate` > DATE_SUB( NOW( ) , INTERVAL 1 WEEK )  GROUP BY ccatId ORDER BY count DESC

It return something like this ,

+--------+-------+
| ccatId |  count|
+--------+-------+
|   12   |    2  |
|  117   |    2  |
+--------+-------+

Upvotes: 0

Nijraj Gelani
Nijraj Gelani

Reputation: 1466

Try this query :

SELECT ccatId, count(ccatId) AS rank FROM `table_name` GROUP By ccatId ORDER BY rank DESC;

It returns something like this,

+--------+-------+
| ccatId |  rank |
+--------+-------+
|   12   |    3  |
|  117   |    3  |
|   37   |    1  |
+--------+-------+

If you want entries from last week only then just put a WHERE clause.

Upvotes: 1

Related Questions