Ok...
To convert your statement to joins, the one thing we need is to know how we can join each table together.
If such a join cannot be done, then your structure cannot be used in a joined query.
As I see the table structure, I tend to think that indeed, no join can be made between the tables.
Am I right?
If this is the case, I will work on the union here. You can tell me later if there is something to be done for a joined query.
First, it would be better to name the column you want in the select literally, rather than using "*".
If you have to do an operation on the table definition, you can have the order of the columns that change, and this will be a problem will the union, because you cannot mix a column of type timestamp with a float (for example).
In a union, the order
and the type of the columns are important.
But keeping it that way for the moment, your query should be written
Code:
SELECT *, src='blog'
FROM blog
WHERE categories LIKE '%Music%' AND disclude_categories!=1
UNION
SELECT *, src='news'
FROM news
WHERE categories LIKE '%Music%' AND disclude_categories!=1
UNION
SELECT *, src='reviews'
FROM reviews
WHERE categories LIKE '%Music%' AND disclude_categories!=1
Now, this should work. I have added the "src" column to the selects.
I left out the order by clause, because we will need to go further in "advanced sql" for that.
Test it already, and see if the results are what you want.
Now, for the order by, we cannot simply give it like you did.
Because, first, "date" is probably a field present in every tables, right?
In that case, you will need to tell the DB which table is the reference.
I suspect that you want to sort on the whole set of data the selects are returning.
In that case, you need to enclose this query up there into another one.
So, the "union" query become a subquery, and you tell the DB engine what to do with it.
Don't be scared, it's as simple as a, b, c.
Code:
SELECT *
from (
SELECT *, src='blog'
FROM blog
WHERE categories LIKE '%Music%' AND disclude_categories!=1
UNION
SELECT *, src='news'
FROM news
WHERE categories LIKE '%Music%' AND disclude_categories!=1
UNION
SELECT *, src='reviews'
FROM reviews
WHERE categories LIKE '%Music%' AND disclude_categories!=1
) as all
ORDER BY all.`date` DESC LIMIT 0,$music_limit;
As you can see, really easy...
This quind of query has to be read from the inner to the outer.
What the db does in that case, is treating the result of the subquery like a table.
From there, you can do whatever you want to. And that's a glimpse of the power a DB engine can leverage for you...
First, the union query will be ran by the DB.
With every rows that this query returns, it will apply the "order by" clause.
As you have all the rows in the subquery, you will effectively order them over the field "date" of each union'ed selects.
Now, if you want to have no more than 15 rows of each tables, and not the 15 of the whole rows mixed up, then you would write it:
Code:
SELECT *
from (
SELECT *, src='blog'
FROM blog
WHERE categories LIKE '%Music%' AND disclude_categories!=1
ORDER BY `date` LIMIT 0,$music_limit
UNION
SELECT *, src='news'
FROM news
WHERE categories LIKE '%Music%' AND disclude_categories!=1
ORDER BY `date` LIMIT 0,$music_limit
UNION
SELECT *, src='reviews'
FROM reviews
WHERE categories LIKE '%Music%' AND disclude_categories!=1
ORDER BY `date` LIMIT 0,$music_limit
) as all
ORDER BY all.`date` DESC;
This way, the subquery gives you the 15 last items of each tables, and your outer query order them all by the date.
One last thing.
Keep in mind that some words are reserved in mysql, and should not be used as column name.
"date" is one of them. If you still insist to use it, you must put it between the mysql escape character which is `(backtick).
See this page of the mysql documentation:
http://dev.mysql.com/doc/refman/5.5/...ved-words.html
EDIT:
Ok, as of reading that page, I see that
Quote:
|
MySQL allows some keywords to be used as unquoted identifiers because many people previously used them.
|
And that "date" is one of those keywords. So no need to use backtick in that specific case...