Here is the problem, I’m not sure how to tackle it..let’s say I have something like this:
SELECT news_key FROM tblNews (WHERE news_key>100);
while ($row = mysql_fetch_array($rid)) {
$key=$row['news_key'];
SELECT articles from tblMain WHERE news_key=$key;
while ($row2 = mysql_fetch_array($rid2)) {
echo ($row['articles']);
}
}
Now lets say I want to sort the articles by popularity,.so I could change my second query to something like this…
SELECT articles, popularity from tblMain WHERE
news_key=$key ORDER BY popularity DESC;
But now that isn’t going to work..because it’s just going to grab records as the first loop finds them. Also, I’ve made a really simple illustration here, and I know joining tables on the first query would do this, but the first query in my real life example has a lot of counting and grouping, so I fear that is not an option. I should also mention I have pagination involved, so the first query gets re-queried each time a page is selected…
Any advice?
You have to use a
JOINin this case to join both tables together and order the result. Your query should look something like this:This way you should be able to get the correct result.