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-21-2009, 03:52 PM Column 'categories' in where clause is ambiguous
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
I'm having trouble selecting multiple tables from the mysql SELECT command in PHP. All of the tables have the same columns but with different content. The code I'm trying to use is:

Code:
$query = "select * from blog JOIN news JOIN reviews WHERE categories LIKE '%Movies%' AND disclude_categories!=1 ORDER BY date DESC LIMIT 0,$movies_limit";
But the error I get is:

Code:
Column 'categories' in where clause is ambiguous
bswinnerton is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-21-2009, 04:14 PM Re: Column 'categories' in where clause is ambiguous
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
it means that a column specified in the where clause exists in more than ONE table.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-21-2009, 04:49 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
specify the name of the table in your where clause:
Code:
select x,y,z
from tableA
inner join tableB on tableA.field=tableB.field
where tableA.AnotherField='z'
or use aliases on your joins:
Code:
select x,y,z
from tableA as A
inner join tableB as B on A.field=B.field
where A.AnotherField='z'
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 12-22-2009, 08:51 AM Re: Column 'categories' in where clause is ambiguous
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
Do I have to manually tell it which fields that I want it to join? The tables are exactly the same (with the exception of the content of course).

For example this is my table structure:
Code:
+---------------------+------------+------+-----+---------------------+----------------+
| Field               | Type       | Null | Key | Default             | Extra          |
+---------------------+------------+------+-----+---------------------+----------------+
| id                  | int(11)    | NO   | PRI | NULL                | auto_increment | 
| date                | datetime   | NO   |     | 0000-00-00 00:00:00 |                | 
| title               | text       | NO   |     | NULL                |                | 
| author              | text       | NO   |     | NULL                |                | 
| content             | text       | NO   |     | NULL                |                | 
| categories          | text       | NO   | MUL | NULL                |                | 
| disclude_categories | tinyint(1) | NO   |     | 0                   |                | 
| theloop             | tinyint(1) | NO   |     | 0                   |                | 
| deleted             | tinyint(1) | NO   |     | 0                   |                | 
+---------------------+------------+------+-----+---------------------+----------------+
bswinnerton is offline
Reply With Quote
View Public Profile
 
Old 12-22-2009, 09:22 AM 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
Quote:
Do I have to manually tell it which fields that I want it to join?
Yes, you do.
Otherwise, how will the db engine know how to map the tables ?

Or you could use UNION queries
Code:
select x,y,z
from tblA
union 
select x,y,z
from tblB
union
select x,y,z
from tblC
The only thing using union is that you have to have the same number of fields, and the same type of data for each query in the union
http://dev.mysql.com/doc/refman/5.0/en/union.html
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 12-22-2009 at 09:25 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 12-22-2009, 10:52 AM Re: Column 'categories' in where clause is ambiguous
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
Hmm, well they do have the same number of fields and data types, so union should work. However now, my "where" statement is broken:

Code:
$query = "SELECT * FROM blog UNION SELECT * FROM news UNION SELECT * FROM reviews WHERE categories LIKE '%Something%' AND disclude_categories!=1 ORDER BY date DESC LIMIT 0,$music_limit"
It simply prints out everything, ignoring if categories contains "Something".
bswinnerton is offline
Reply With Quote
View Public Profile
 
Old 12-22-2009, 11:40 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
In described case WHERE condition will be applied to only last table. The correct way is
select * from table1 where $condition
union
select * from table2 where $condition
union
select* from table3 where $condition
__________________

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!
 
Old 12-22-2009, 11:43 AM Re: Column 'categories' in where clause is ambiguous
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
Quote:
Originally Posted by mtishetsky View Post
In described case WHERE condition will be applied to only last table. The correct way is
select * from table1 where $condition
union
select * from table2 where $condition
union
select* from table3 where $condition
Ohhh, that makes perfect sense. Should I do the same for the "and" and "order by" statements as well?
bswinnerton is offline
Reply With Quote
View Public Profile
 
Old 12-22-2009, 11:50 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
select * from table1 where a = $a and b = $b order by a
union
select * from table2 where b = $b and c = $c order by b
union
select * from table3 where c = $c and d = $d order by c

Union unites data from result sets as if they were obtained with three separate queries. On DB level it is in fact three separate queries.
__________________

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!
 
Old 12-22-2009, 11:52 AM Re: Column 'categories' in where clause is ambiguous
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
That's very interesting.

Now I'm curious, when I print the content of the query, is there a mysql/php function (or any way for that matter) to echo the current table that it's gathering the data from?
bswinnerton is offline
Reply With Quote
View Public Profile
 
Old 12-22-2009, 12:06 PM Re: Column 'categories' in where clause is ambiguous
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
At the moment you display the data it is all already "gathered". Still if you need to know which row went from which table u can specify that in query:

select *, 't1' as source from table1 where a = $a and b = $b order by a
union
select *, 't2' as source from table2 where b = $b and c = $c order by b
union
select *, 't3' as source from table3 where c = $c and d = $d order by c

After that in your result set you will have field 'source' which contains the reference to corresponding table.
__________________

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!
 
Old 12-22-2009, 04:08 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
Edit:
or, you can use the solution mtishetsky gave you.
Sorry mtishetsky, I skipped your reply

Quote:
Originally Posted by bswinnerton View Post
That's very interesting.

Now I'm curious, when I print the content of the query, is there a mysql/php function (or any way for that matter) to echo the current table that it's gathering the data from?

Code:
select x,y,z,src='tblA'
from tblA
union 
select x,y,z,src='tblB'
from tblB
union
select x,y,z,src='tblC'
from tblC
And now, you have a field "src" with the table the row come from.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 12-22-2009 at 04:11 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 12-23-2009, 12:33 AM Re: Column 'categories' in where clause is ambiguous
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
Perfect. But how would I actually echo the src in php?

echo 'src'; I assume wouldn't work. What's the correct way?
bswinnerton is offline
Reply With Quote
View Public Profile
 
Old 12-23-2009, 12:54 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
How do u echo all other fields from DB result set?
__________________

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!
 
Old 12-23-2009, 07:20 AM 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
Aha, you haven't understood the power of union yet...
When you send an union'ed query to the db, it returns you 1 dataset, like any other query.
You cannot by itself get the distinction from which select the row comes.

By adding a column with that information, you simply extend the result set by 1 column, that gives you this information.

To get them, you do the same thing that you do to get other columns:
PHP Code:
$res=mysql_query("the union query");
while(
$o=mysql_fetch_object($res)){
  print 
'field x:'.$o->x;
  print 
'field y:'.$o->y;
  print 
'field z:'.$o->z;
  print 
'field src:'.$o->src;
  print 
'<hr/>';

__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 12-23-2009, 08:37 AM Re: Column 'categories' in where clause is ambiguous
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
I see. Well I was just wondering if there was a simple MySQL or PHP variable that already knows the table that it's working in, I thought that might be a little bit easier.

Anyways, I tried the ",src='table1'" that you recommended, but when I do that I get the error: "Unknown column 'src' in 'field list'". I don't have to actually create a new column in the MySQL database do I?

I'm starting to doubt whether or not this is even the best way of going about it. Is there another way that you would recommend besides union? If I do decide to change some of the columns around in each table it will break the union statement, is there perhaps any way that I can use multiple tables no matter their column structure?
bswinnerton is offline
Reply With Quote
View Public Profile
 
Old 12-23-2009, 09:40 AM Re: Column 'categories' in where clause is ambiguous
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Why not just run three seperate queries then?
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 12-23-2009, 09:51 AM Re: Column 'categories' in where clause is ambiguous
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
Quote:
Originally Posted by lizciz View Post
Why not just run three seperate queries then?
Excuse my ignorance I'm very new to PHP. That sounds like a great idea, how can I organize the sum of the three queries by date?

EDIT: I have a date column in the columns of my MySQL data.
bswinnerton is offline
Reply With Quote
View Public Profile
 
Old 12-23-2009, 10:46 AM 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
Quote:
I don't have to actually create a new column in the MySQL database do I?
No, you can dynamically create columns in a select by giving "column name"="value"

Code:
select msg='hello world';
Is perfectly valid.
I use the same principle in the union query.
Quote:
is there perhaps any way that I can use multiple tables no matter their column structure?
Yes, by using joins and giving in the select the fields you want to output and the tables they come from, like in you first query

Quote:
how can I organize the sum of the three queries by date?
You cannot easily. If you go that road, you will need to parse with PHP each resultset, and put them in another one in the order you want to.
I'd say you better stick with either the union or the join query.

Quote:
Anyways, I tried the ",src='table1'" that you recommended, but when I do that I get the error: "Unknown column 'src' in 'field list'".
In that case, you have a syntax error.
Paste your query here, and we'll tell you where the problem is.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 12-23-2009, 11:08 AM Re: Column 'categories' in where clause is ambiguous
bswinnerton's Avatar
Skilled Talker

Posts: 57
Location: Montville, CT
Trades: 0
Okay, then in that case (I'm sorry!), I think that I want to go back to the join statement.

Here is my current UNION statement, how would I go about converting it to join?

Code:
$query = "SELECT * FROM blog WHERE categories LIKE '%Music%' AND disclude_categories!=1 UNION SELECT * FROM news WHERE categories LIKE '%Music%' AND disclude_categories!=1 UNION SELECT * FROM reviews WHERE categories LIKE '%Music%' AND disclude_categories!=1 ORDER BY date DESC LIMIT 0,$music_limit";
(and just for clarification):
I have three tables: blog, news, and reviews

and the table structure of each reads:

Code:
+---------------------+------------+------+-----+---------------------+----------------+
| Field               | Type       | Null | Key | Default             | Extra          |
+---------------------+------------+------+-----+---------------------+----------------+
| id                  | int(11)    | NO   | PRI | NULL                | auto_increment | 
| date                | datetime   | NO   |     | 0000-00-00 00:00:00 |                | 
| title               | text       | NO   |     | NULL                |                | 
| author              | text       | NO   |     | NULL                |                | 
| content             | text       | NO   |     | NULL                |                | 
| categories          | text       | NO   | MUL | NULL                |                | 
| disclude_categories | tinyint(1) | NO   |     | 0                   |                | 
| theloop             | tinyint(1) | NO   |     | 0                   |                | 
| deleted             | tinyint(1) | NO   |     | 0                   |                | 
+---------------------+------------+------+-----+---------------------+----------------+

Last edited by bswinnerton; 12-23-2009 at 11:12 AM..
bswinnerton is offline
Reply With Quote
View Public Profile
 
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.73398 seconds with 12 queries