mysql - Multi-conditional join -
i bet easy question mysql chops bit rusty. need little generating proper query following scenario multi conditional join. have 1 table attributes (think tags) , table have list of records. running problems when want match page has more 1 attribute defined in query.
+--------------------------------------+ | attributes | +-----------+------------+-------------+ | name | value | page_id | +-----------+------------+-------------+ | type food 2 | | fruit apple 2 | | color green 2 | | type frog 3 | | color green 3 | +--------------------------------------+ +--------------------------------------+ | pages | +--------------+-----------------------+ | page_id | title | +--------------+-----------------------+ | 2 granny smith | | 3 kermit frog | +--------------------------------------+
use cases
query: type+food results: granny smith query: color+green results: granny smith, kermit frog query: type+food , color+green results: granny smith
this query playing with, not working:
select page.page_id, page.title page_attributes left join page on (page_attributes.page_id = page.page_id) (page_attributes.name = 'color' , page_attributes.value = 'green') , (page_attributes.name = 'type' , page_attributes.value = 'food')
along same lines nitro has, keep criteria attributes on clause. also, make sure have index on attributes table on ( page_id, name, value ) joins optimized.
select page.page_id, page.title page inner join page_attributes a1 on page.page_id = a1.page_id , a1.name = 'color' , a1.value = 'green' inner join page_attributes a2 on page.page_id = a2.page_id , a2.name = 'type' , a2.value = 'food' limit 10
as can see, want add "criteria", copy/past entire join. either finds match , allows include, or doesn't , ignores page record.
Comments
Post a Comment