Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Column 'categories' in where clause is ambiguous
Old 12-23-2009, 03:39 PM Re: Column 'categories' in where clause is ambiguous
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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...
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 12-23-2009 at 03:47 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
 
Register now for full access!
Old 12-24-2009, 01:42 AM Re: Column 'categories' in where clause is ambiguous
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
If you have three different tables with same structure i can suggest placing all data into one table and add a field to distinct types of records, e.g. news, article or review.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to Column 'categories' in where clause is ambiguous

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11719 seconds with 11 queries