cluelessPHP wrote:wouldn't union count as two queries?
In a sense yes because it pretty much does two queries and combines the results, except that the database engine does it for you. However, because the two tables have no relationship, that's the best that can be done. What you suggested in post #4 is a Cartesian join, which makes no sense here (and usually isn't desired).
For example, let's suppose that there are two tables News and Articles, each with the column added_at, and News has the column headline that is to be displayed whereas Articles has the column title that is to be displayed. Modifying your query, we might get:
SELECT *
FROM News, Articles
ORDER BY headline, title DESC
LIMIT 5
Immediately you can see a problem: it makes no sense to order by title in descending order since you want to order by the added_at in descending order in order to get the most recent, but there are two added_at columns to consider, i.e., one from News and one from Articles. Let's bite the bullet and order using both of them:
SELECT *
FROM News, Articles
ORDER BY News.added_at DESC, Article.added_at DESC, headline, title
LIMIT 5
Great. Let's suppose we have this data:
[code]News
added_at | headline
-----------+---------
2017-10-15 | News A
2017-10-13 | News B
2017-10-11 | News C
Articles
added_at | title
-----------+----------
2017-10-14 | Article X
2017-10-12 | Article Y
2017-10-10 | Article Z[/code]
Now we run the second modified version of your query, before the LIMIT is applied:
headline | title | News.added_at | Article.added_at
---------+-----------+---------------+-----------------
News A | Article X | 2017-10-15 | 2017-10-14
News A | Article Y | 2017-10-15 | 2017-10-12
News A | Article Z | 2017-10-15 | 2017-10-10
News B | Article X | 2017-10-13 | 2017-10-14
News B | Article Y | 2017-10-13 | 2017-10-12
News B | Article Z | 2017-10-13 | 2017-10-10
News C | Article X | 2017-10-11 | 2017-10-14
News C | Article Y | 2017-10-11 | 2017-10-12
News C | Article Z | 2017-10-11 | 2017-10-10
Applying the limit:
headline | title | News.added_at | Article.added_at
---------+-----------+---------------+-----------------
News A | Article X | 2017-10-15 | 2017-10-14
News A | Article Y | 2017-10-15 | 2017-10-12
News A | Article Z | 2017-10-15 | 2017-10-10
News B | Article X | 2017-10-13 | 2017-10-14
News B | Article Y | 2017-10-13 | 2017-10-12
Do you see the problem now? The Cartesian join has given you every possible combination of News and Article before the LIMIT, but that's obviously not what is wanted as after the limit you still don't have the 5 most recent news or article, at least not in a form that can be meaningfully interpreted. What is wanted is something like this:
headline_or_title | added_at
------------------+-----------
News A | 2017-10-15
Article X | 2017-10-14
News B | 2017-10-13
Article Y | 2017-10-12
News C | 2017-10-11
Typically, when faced with a Cartesian join, the solution is modify it such that a more restrictive join is performed, but as the two tables have no relationship, such a join is nonsensical: there's nothing to put in the ON or WHERE join clauses. Therefore, one can only ditch this query entirely, which means that one is left with the original two queries... hence the UNION, or if that cannot be done, then manually doing a union in PHP after running the two queries.