matthewhse — 2012-06-08T22:39:50-04:00 — #1
I'm not that experienced with MySQL, but I thought I knew enough to do what I need this time. But I'm getting completely unexpected results and can't figure out the problem.
Here's the query I'm using:
SELECT channels.name, channels.description, categories.name, categories.description FROM channels, categories WHERE channels.title='$var' OR categories.title='$var'
$var contains a string that will be the 'title' of either a channel or a category. The query is intended to check both tables and return the applicable row from whichever table it finds a match for $var in the 'title' field. There are no duplicate 'title' field values between the two tables.
The actual results I'm getting are a full list of all rows in whichever table it shouldn't have found a match in at all. In other words, when $var should match a single row in the 'channels' table, the query actually returns ALL rows in the 'categories' table, and vice-versa.
What do I need to modify to get the expected results?
Thanks for any ideas,
r937 — 2012-06-08T22:48:32-04:00 — #2
you forgot to join the matching rows
change this --
FROM channels, categories
WHERE channels.title='$var' OR categories.title='$var'
to this --
ON <font color='"#0000FF"'>categories.id </font>= <font color='"#0000FF"'></font><font color='"#0000FF"'>channels.category_id</font>
WHERE channels.title = '$var'
OR categories.title = '$var'
see those two columns marked in blue? i guessed at the names
matthewhse — 2012-06-09T00:59:30-04:00 — #3
Thank you for such a quick reply. Unfortunately, I'm not sure JOIN is what I'm after because the two tables in question have no relationship, and really, no relevance to one another. I just need to know which table $var is found in.
I did try the code you suggested, but it also yielded strange results by fetching data from both tables from rows where the ID's matched one another. I only want to retrieve data from one row from whichever table matches $var - the other fields I'm using for SELECT can remain null as they won't be used anyway.
Is there any way to accomplish this?
guido2004 — 2012-06-09T03:21:08-04:00 — #4