Selecting from table A using grouped/sorted data from table B
Basically what I need to do is find trending pages. I have one table that holds a list of page IDs and the timestamp of creation (they are created when a person visits the aforementioned page).
SELECT * FROM trends WHERE timestamp > (NOW() - INTERVAL 7 DAY) GROUP BY pageId ORDER BY COUNT(id) DESC
This will return the pageId in trending order, but now I need to use those pageIds to se开发者_开发技巧lect from another table (one that contains the actual pages).
I could use a php while($row = fetch array)
loop to select rows from the other table, but I'd like to do all of this in only one query.
I am not sure of the syntax needed to do that however...
You need to join the two tables trend
and pages
on the pageId
. See the MySQL reference for the JOIN
syntax.
精彩评论