Answer the question
In order to leave comments, you need to log in
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')
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
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
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 questionAsk a Question
731 491 924 answers to any question