30

I have a table like this:

id  image_id  style_id  style_value
-----------------------------------
1   45        24        red
1   45        25        big
1   47        26        small
1   45        27        round
1   49        28        rect

I want to take image_id column if:

  • style_id = 24 and style_value = red
  • style_id = 25 and style_value = big
  • style_id = 26 and style_value = round

I have make a query like this:

$query = mysql_query("SELECT image_id FROM list WHERE (style_id = 24 AND style_value = 'red') AND (style_id = 25 AND style_value = 'big') AND (style_id = 27 AND style_value = 'round')

But I couldn't get any result. When I make this sample with OR, it works well. But I have to do this with AND. Because I need image id s which are both "red, big and rect".

I have made lots of search with Google but couldn't fine any solution.

serenesat
  • 4,611
  • 10
  • 37
  • 53
Bugra YUKSEL
  • 379
  • 1
  • 3
  • 7
  • Is there any dependency between style_id and style_value? That is, does style_id 24 mean "red", while style_id 25 means "big"? – mdoyle Dec 26 '12 at 21:57
  • Yes, i have another table named "styles". And a style has 5-6 different options. For example: style_id 24 has 5 different options (red, green, yellow, ...). – Bugra YUKSEL Dec 26 '12 at 22:00
  • Could you provide a `SHOW CREATE TABLE list\G` and `SHOW CREATE TABLE styles\G`, and also provide a few sample rows from the table `styles`? – mdoyle Dec 26 '12 at 22:09
  • Table 1: images id - image_name 1 - abc 2 - def 3 - ghi ... Table 2: Styles id - style_name - style_values 1 - color - (red-green-blue-etc.) 2 - shape - (round, rect, etc.) I am collection style values as a serialized text in one column. Whenever i create an image i am selecting color from a selectbox and i insert this relation to table 3: Table 3: relations (as i mentioned in my post) id - image_id - style_id - style_value – Bugra YUKSEL Dec 26 '12 at 22:16
  • Can you provide a sample of the expected returned data? – Lock Dec 26 '12 at 22:21
  • I just only want to take image_id from association table. For example as i mentioned in my first post: I want to take "red, big and round" image_id. It means: 45 (according to table) – Bugra YUKSEL Dec 26 '12 at 22:27
  • So style_id 24 is always color, style_id 25 is always size, and style_id 26 is always shape. I think I am following. – mdoyle Dec 26 '12 at 22:30
  • So with the above, would the query not return any rows, as style_id 26 does not equal 'rect'? – Lock Dec 26 '12 at 22:32
  • Yes friend, you are right. style_id 24 is always color, style 25 is always size. – Bugra YUKSEL Dec 26 '12 at 22:32
  • @Lock, You are right. I have made a mistake while writing to stackoverflow. Now, i have fixed it. When style_id 27, it must be round. – Bugra YUKSEL Dec 26 '12 at 22:35

6 Answers6

52

I think that you are after this:

SELECT image_id
FROM list
WHERE (style_id, style_value) IN ((24,'red'),(25,'big'),(27,'round'))
GROUP BY image_id
HAVING count(distinct style_id, style_value)=3

You can't use AND, because values can't be 24 red and 25 big and 27 round at the same time in the same row, but you need to check the presence of style_id, style_value in multiple rows, under the same image_id.

In this query I'm using IN (that, in this particular example, is equivalent to an OR), and I am counting the distinct rows that match. If 3 distinct rows match, it means that all 3 attributes are present for that image_id, and my query will return it.

fthiella
  • 48,073
  • 15
  • 90
  • 106
16
SELECT a.image_id 
FROM list a
INNER JOIN list b
   ON a.image_id = b.image_id
   AND b.style_id = 25
   AND b.style_value = 'big'
INNER JOIN list c
   ON a.image_id = c.image_id
   AND c.style_id = 27
   AND c.style_value = 'round'
WHERE a.style_id = 24 
   AND a.style_value = 'red'
bobwienholt
  • 17,420
  • 3
  • 40
  • 48
10

You will never get a result, it's a simple logic error.

You're asking your database to return a row which has style_id = 24 AND style_id = 25 AND style_id = 26. Since 24 is niether 25 nor 26, you will get no result.

You have to use OR, then it makes some sense.

akashivskyy
  • 44,342
  • 16
  • 106
  • 116
  • But, there're also both, image_id (45) => style_id (24), image_id (45) => style_id (25), image_id (45) => style_id (26)... It means that table have 3 different rows with same image_id. – Bugra YUKSEL Dec 26 '12 at 22:55
  • So you should check also for the same image_id. I'm not quite sure what's the purpose of your SQL. – akashivskyy Dec 26 '12 at 23:01
4

This might be what you are after, although depending on how many style_id's there are, it would be tricky to implement (not sure if those style_id's are static or not). If this is the case, then it is not really possible what you are wanting as the WHERE clause works on a row to row basis.

WITH cte as (
  SELECT
    image_id,
    max(decode(style_id,24,style_value)) AS style_colour,
    max(decode(style_id,25,style_value)) AS style_size,
    max(decode(style_id,27,style_value)) AS style_shape
  FROM
    list
  GROUP BY
    image_id
)
SELECT
  image_id
FROM
  cte
WHERE
  style_colour = 'red'
  and style_size = 'big'
  and style_shape = 'round'

http://sqlfiddle.com/#!4/fa5cf/18

Lock
  • 5,422
  • 14
  • 66
  • 113
  • I have tried in sqlfiddle, but these values aren't static. So i have to convert to into a dynamic version. If i cannot find any practical solution, i can try your solution. Thanks Lock. – Bugra YUKSEL Dec 26 '12 at 22:56
2
select unique red24.image_id from 
( 
    select image_id from `list` where style_id = 24 and style_value = 'red' 
) red24
inner join 
( 
    select image_id from `list` where style_id = 25 and style_value = 'big' 
) big25
on red24.image_id = big25.image_id
inner join 
( 
    select image_id from `list` where style_id = 27 and style_value = 'round' 
) round27
on red24.image_id = round27.image_id
Keith John Hutchison
  • 4,955
  • 11
  • 46
  • 64
  • thanks. Your code works very well. I just only remove "unique" parameter according my need. Everything is okay right now. Thanks again and have a nice work. – Bugra YUKSEL Dec 27 '12 at 07:55
2

May be using this query you don't get any result or empty result. You need to use OR instead of AND in your query like below.

$query = mysql_query("SELECT image_id FROM list WHERE (style_id = 24 AND style_value = 'red') OR (style_id = 25 AND style_value = 'big') OR (style_id = 27 AND style_value = 'round');

Try out this query.

Ajay Gupta
  • 2,867
  • 23
  • 28