PDA

View Full Version : Problem! MySQL query with two INNER JOINS and WHERE with two conditions


blackweldermhe400113
02-03-2010, 06:33 AM
I'm thinking this isn't that complicated of an issue, but I can't seem to make it work properly. First, I'm using php and have a total of three tables in my database. The first two tables are simple. The first being the "category" table containing an id and name, and the second being the "collection" table containing again an id and a name. The master table of "products" contains two foreign keys that relate to the id's of both the category and collections tables. Through the dreamweaver recordset panel in advanced mode I'm querying the product table and using two INNER JOINS to combine the category, collection, and products tables together. I'm using a WHERE statement to pull two parameters with an OR. Example code below.

SELECT *
FROM product INNER JOIN category ON idcategory_product=id_category INNER JOIN collection ON idcollection_product=id_collection
WHERE idcategory_product=colname || idcollection_product=colname2
ORDER BY id_product DESC

Variables:

Name: colname
Type: numeric
Default Value: 1
Runtime Value: $_GET['id_category']

Name: colname2
Type: numeric
Default Value: 1
Runtime Value: $_GET['id_collection']

What I'm trying to do is set the navigation on the index page to jump to the products page and pull the appropriate content. So, when a link in the category column on the index page is selected it will jump to a products page and pull all products of that category and the same with the collections, when a collection is selected it will jump to the same products page and list all items within that collection. The nav on the index is being pulled dynamically from the above mentioned category and collections tables. It is then linked to the products page with set parameters for the id of the given table.

The above query seems to work fine with just one of the WHERE conditions but when I try and set both it just wants to dump all data entries in descending order and not filter at all.

Does anyone have any ideas on how to make this work properly? I should be able to fit it all into the same query.

blackweldermhe400113
02-03-2010, 08:45 AM
The navigation on the index page for both the category and collections is a list created by pulling the name of the table item, using a repeat region ordered by ascending order, and then it's filtered to the products page by the id of the table item for the link. So for instance, the first item in the categories column would be "/products.php?id_category=1" or something to that effect. It might not be the exact syntax that I'm using, but I'm almost 100% based off of what I can remember at this time. I'm not able to look at my project seeing as how I'm at work.

Jason Byrnes
02-03-2010, 09:48 AM
your SQL Syntax for the where clause is off:
WHERE idcategory_product=colname || idcollection_product=colname2


should be:
WHERE idcategory_product=colname OR idcollection_product=colname2

blackweldermhe400113
02-03-2010, 10:06 AM
I tried the OR operator and it still seems to be doing the same thing. When you select one of the category links from the index page the products page dumps all items in descending order, but the odd thing is when you select one of the collection items it seems to be partially working. Like say i select item one in the collections column with an id=1, the products page will dump the items within that collection, can't tell if that has something to do with the order in which the item entries where made. Even stranger, if I select the second item in the collections column with an id=2 the products page will supply the first three items as items that have a category id=1 / collection id=1 then supply all the right items for the collection id=2. So it queries somewhat correctly. Not sure what's going. Can you think of anything else?

Jason Byrnes
02-03-2010, 01:09 PM
Set the default values for each of the parameters to -1. the default value should be a value that does not exist.

blackweldermhe400113
02-03-2010, 03:12 PM
That was the problem. The variable's default value for colname and colname2 needed to be set to -1. Thanks for your time and efforts. Much appreciated!