0

I'm looking for an alternative SQL query to the following statement:

SELECT COUNT(`id`) FROM `products`
    WHERE 
      `category` = "Motors" AND 
      `id` IN(SELECT `id` FROM `products` WHERE 
                `Horsepower` > 200 AND 
                `id` IN(SELECT `id` FROM `products` WHERE 
                          `Manufacturer` = "Baldor"))

As you can see, theoretically (because I didn't specifically test it) the sub queries all select product IDs. Then, a COUNT is generated from the final list of product IDs. This method can get slow when many nested queries are there.

I'm working on a filtering system. So one or more filters and sub filters are selected and a COUNT is generated based on the current selection.

It will be fantastic if I can create a SQL statement to return the number of products based on any number of sub filters (without creating "WHERE IN" clauses).

The above query can be simply made to use only the WHERE...AND clause. The problem lies in using something like the following as sub queries because it needs to select multiple conditions:

SELECT `products`.`id` FROM `products` 
  LEFT JOIN `attributes` ON `attributes`.`product_id` = `products`.`id`
  WHERE `attributes`.`Label` = "Horsepower" AND
        `attributes`.`Value` > 200
SELECT `products`.`id` FROM `products` 
  LEFT JOIN `attributes` ON `attributes`.`product_id` = `products`.`id`
  WHERE `attributes`.`Label` = "Category" AND
        `attributes`.`Value` = "Motors"
3
  • 3
    Seems rather pointless. why not just a single where category=motors and horsepower > 200 and manufacturer = balor?
    – Marc B
    Jul 3, 2014 at 19:15
  • What do you need sub queries for? Can't you just write one where clause?
    – ragol
    Jul 3, 2014 at 19:17
  • You can literally just delete lines 4 and 6, and the 2 trailing )s. Jul 3, 2014 at 19:20

1 Answer 1

3

Why do you need the in? Just AND the requirements together

select count(`id`) from `products` where category = "Motors" and `HorsePower` > 200 and `Manufacturer` = "Baldor"

The usual technique to do this in code, assuming you're not using an ORM (Which, frankly, given that you're asking the question you probably should be) would be something like this

#Python
conds = ['category = "Motors"', '`Horsepower` > 200']
query = 'SELECT COUNT(`id`) FROM `products` WHERE ' + ' and '.join(conds)

If you need to pull from a second table you can do something like:

select count(products.id) from products left join extra on extra.product_id = products.id where products.category = 'foo' and extra.fielda = 'blah' and extra.fieldb = 'bar'

You can use additional joins if you need multiple tables.

2
  • It's that simple when dealing with the WHERE clause shown. Now I see where the complexity lies. It lies in each sub query selecting conditions from other tables. So it might select the Label and Value as a single condition. It's almost as if it has to select products first before moving on to the next filter. Jul 3, 2014 at 19:26
  • 1
    @CodyHelscel . . . This question is pretty clearly about pulling from one table. If you have an enhanced question about pulling from multiple tables, I would suggest that you accept this answer and write another question, with sample data and desired results. Jul 3, 2014 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.