H
H
Hazrat Hajikerimov2014-02-15 13:07:35
PHP
Hazrat Hajikerimov, 2014-02-15 13:07:35

SQL query not working?

There is a task, creation of the catalog with sorting of the goods.
I ran into such a problem that if from the same group and in this example it is atributes.names and AND atributes.value, you need to get different data.
Compiled a SQL query, the one below. The trouble is that, as I understand it, it is impossible to take several values ​​like the following from the same group:

AND atributes.names = "Производитель" 
AND atributes.value IN ('ZOTAC','PALIT','ASUS') 
AND atributes.names = "Графический процессор"  
AND atributes.value IN ('nVidia GeForce GTX 660')

So how do I select these values, and that they are all required, and not as in the cases with IN (value1, value2) where if there is no value2, then it will return value1.
Help to make a sql query that meets the above requirements.
The full request is below.
SELECT * FROM `catalog` 
LEFT OUTER JOIN atributes ON catalog.art = atributes.art 
WHERE catalog.catalog_3 = "Видеокарты" 
AND catalog.price != "0" 
AND catalog.avail != "0" 
AND atributes.names = "Производитель" 
AND atributes.value IN ('ZOTAC','PALIT','ASUS') 
AND atributes.names = "Графический процессор"  
AND atributes.value IN ('nVidia GeForce GTX 660') 
ORDER BY price ASC LIMIT 0, 15

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2014-02-15
@hazratgs

SELECT  * FROM `catalog` as `c` 
    INNER JOIN `atributes` AS `a1` ON `c`.`art` = `a1`.`art` 
    INNER JOIN `atributes` AS `a2` ON `c`.`art` = `a2`.`art` 
    WHERE `c`.`catalog_3` = "Видеокарты" 
        AND `c`.`price` != "0" 
        AND `c`.`avail` != "0" 
        AND `a1`.`names` = "Производитель" 
        AND `a1`.`value `IN ('ZOTAC','PALIT','ASUS') 
        AND `a2`.`names` = "Графический процессор"  
        AND `a2`.`value` IN ('nVidia GeForce GTX 660') 
        ORDER BY price ASC 
        LIMIT 0, 15

But you still learn how to express your thoughts correctly in Russian, it will come in handy.

V
VeMax, 2014-02-15
@VeMax

In addition to AND, there is also OR and brackets that indicate priority)

S
Sergey Lerg, 2014-02-15
@Lerg

You can OR between all attributes, then group the result by art, and eventually display only those products whose group size is equal to the number of attributes in the search.
Maybe there is some more beautiful solution
. You can also remake the base. Make your own column for each attribute, while you can still work with them as dynamic ones in the sense that you can make a sufficiently large number of columns with the names attr1, attr2, attr3 ... attr64 and store the mapping of columns and attributes in a separate table. If the column is NULL for a product, do not display it.
By the way, attribute is written with two t's.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question