Need some, help
did somme google and testing
here's a table :
product_id section_id product_type_id brand_id
1 2 5 12
2 4 6 13
3 3 7 12
4 4 5 13
5 3 8 12
I'm looking for a simple query that would give me this result :
the brand_id's that belong to all section_id for some specific section_id
section_id = 4,3
the result would be
brand_id = 12,13
subquery does not give me the result
my only solution for now is
select all from table where
section_id = a
section_id = b
section_id = c
product_type_id = z
parsing the array with php to remove
brand_id that do not intersect
I'd like a sql only solution
SELECT DISTINCT brand_id FROM your_table WHERE section_id IN (3,4)
section_id IN (3,4) is the same as section_id=3 OR section_id=4
SELECT DISTINCT means SELECT, and remove all duplicates.
So for this query without using DISTINCT you would get 13,12,13,12
With distinct you get 13,12
Of course you can add an ORDER BY brand_id to sort the results according to brand_id.
When using PHP you can do something like
$section_ids = array(3,4);
$result = mysql_query('SELECT DISTINCT brand_id FROM your_table WHERE section_id IN ('.implode(',', $section_ids).')');
Does that answer your question?
here is the actual query ( the abreviation are not for english )
y_marques_s AS ms)
y_marques_g AS mg ON
section IN (5,6,16,12) ORDER BY
this is no good
more there is section value
more row will it be in the result
the id of brand (ms.id_g) must be in all section to be returned
the chance that a brand appears is less whne the number of section get biger
Could you post a SHOW CREATE for both tables and maybe some dummy data? I'm not entirely sure what you're trying to achieve.
requested info for helping me
Would you like the brands common to each section specified? If so the methodology would be outlined in the below query.
s.id = b.section_id
s.id IN (5,6,16,12)
COUNT(*) = 4
I attempted to translate that to your schema but the language barrier is proving to be an obstacle.
ms.id_g = mg.id
ms.section IN (5,6,16,12)
COUNT(*) = 4
The having clause provides the necessary filter to remove all brands that don't belong to every section. Just make certain that the comparison is against the number of sections specified in the in condition.
If the relationship between sections and brands is m:n you will need to replace COUNT(*) = 4 with COUNT(DISTINCT ms.section) = 4.
I will test this today,
I'have never used "Group By",
I'm lurning somthing here,
I'm very greatful.