Mazatec
Mazatec

Reputation: 11649

mysql - selecting rows horizontally

I have this mysql table (as below image). I want to select id's grouped by id

where:
datakey = label and datavalue = is_member
and, at the same time where:
datakey = since and datavalue = 20110204

So the end result should just be 105.

How can I achieve this?

enter image description here

Upvotes: 0

Views: 93

Answers (2)

Slowcoder
Slowcoder

Reputation: 2120

It should be possible to achieve this without join. http://www.sqlfiddle.com/#!2/ca8d6/1

SELECT t.id
FROM table_name t
WHERE (datakey = 'Label' AND datavalue = 'is_member') OR (datakey = 'Since' AND datavalue = '20110204')
GROUP BY t.id
HAVING COUNT(datakey) = 2;

Upvotes: 2

scones
scones

Reputation: 3345

this should get what you need

SELECT
  a.id
FROM
  some_table AS a
  JOIN some_table AS b
    ON
      b.id = a.id
WHERE
  a.datakey = 'Label'
    AND
  a.datavalue = 'is_member'
    AND
  b.datakey = 'Since'
    AND
  b.datavalue = '20110204'

The assumption here is, that the field id references the same dataset.

Upvotes: 1

Related Questions