close ad
WARNING PC USERS: Do Not Install the DREAMWEAVER CC 2017 Update »
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

mysql query ignoring part of WHERE statement

Thread began 1/20/2015 6:49 pm by david | Last modified 1/21/2015 2:10 pm by Jason Byrnes | 253 views | 3 replies |

david

mysql query ignoring part of WHERE statement

Hey guys, I've hound myself up against another wall... I have the following select statement ...

"SELECT campain_info.idCampaign, campain_info.couponTitle, campain_info.couponExcerpt, campain_info.couponDetails, campain_info.couponRetailPrice, campain_info.couponPrice, campain_info.amountSavings, campain_info.percentSavings, campain_info.expDate, campain_info.limitedQuantity AS 'Qty', campain_info.image, campain_info.localMarket, campain_info.tags, categories.idCategories, categories.categoryName, categories.categoryIcon, merchants.companyName, merchants.compnayWebsite, order_details.idDetails, Sum( CASE WHEN order_details.idDetails IS NOT NULL THEN 1 ELSE 0 END) AS 'Qty_Sold'
FROM campain_info INNER JOIN categories ON campain_info.categoryId = categories.idCategories INNER JOIN merchants ON campain_info.merchants_idMerchants = merchants.idMerchants LEFT OUTER JOIN order_details ON campain_info.idCampaign = order_details.campain_info_idCampaign
WHERE campain_info.expDate > CURDATE() AND campain_info.localMarket LIKE '$lm' AND campain_info.tags LIKE '$search' AND campain_info.limitedQuantity != 'Qty_Sold'
GROUP BY campain_info.idCampaign"

Everything works perfectly except that the final AND in the WHERE clause doesn't affect the return. For example, one of my campaigns has an available qty of 4, and even though the record set displays 4 has the qnty sold it is still returned in the record set. I've also tried > and <> in place of !=

Can you guys take a look at my select statement and see if I made an error OR if what i'm trying to accomplish can't be done this way?

Sign in to reply to this post

Jason ByrnesWebAssist

you have quantity sold set to return 1 or 0:

Sum( CASE WHEN order_details.idDetails IS NOT NULL THEN 1 ELSE 0 END) AS 'Qty_Sold'

4 is not equal to 0

Also, i don't think you can compare alias values returned from aggregate functions in the where clause, i think you need to perform the aggregate function in the where for it work correctly.

If you need one on one help t figure this query out, I think this is more complex than I can help with in the forum, you should sign up for a premiere support ticket for this:
http://www.webassist.com/premier_request.php

Sign in to reply to this post

david

The quantity sold was set to 1 so that for each row returned that was not NULL, would have the value of 1 and then when summed would create the total number of sold. After some more research I cam across something I've used before but totally forgot about ... the HAVING clause.

HAVING Qty > Qty_Sold

This did exactly what I needed.

Thanks Jason!

Sign in to reply to this post

Jason ByrnesWebAssist

you're welcome.

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...