close ad
Install the LAtest Updates to Work with CC 2017 and CC 2018
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Problem! MySQL query with two INNER JOINS and WHERE with two conditions

Thread began 2/03/2010 9:33 am by blackweldermhe400113 | Last modified 2/03/2010 6:12 pm by blackweldermhe400113 | 5403 views | 5 replies |

blackweldermhe400113

Problem! MySQL query with two INNER JOINS and WHERE with two conditions

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.

Sign in to reply to this post

blackweldermhe400113

I guess I should clarify...

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.

Sign in to reply to this post

Jason ByrnesWebAssist

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

Sign in to reply to this post

blackweldermhe400113

The OR operator doesn't work either.

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?

Sign in to reply to this post

Jason ByrnesWebAssist

Set the default values for each of the parameters to -1. the default value should be a value that does not exist.

Sign in to reply to this post

blackweldermhe400113

Solved!!

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!

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...