lundi 29 juin 2015

SQL query with having

I've some problems with understanding how having works. It's clear to me when I check the count of something, but is this the only use case for it?

For example I have a tasks table and tasks_parameters table which have got columns: key, value, task_id. Now, I would like to select all tasks which have got two parameters:

key = 'type' && value = 'work'
key = 'priority' && value = 'normal'

I was thinking about that query:

select t.id from tasks t
join tasks_parameters p on (p.task_id=t.id)
group by t.id, p.key, p.value
having p.key = 'type' and p.value = 'work' and
       p.key = 'priority' and p.value = 'normal';

but, ofcourse, it doesn't work, because parameter can't have two diffrent values for the key. Is it even possible to query something like this?

Aucun commentaire:

Enregistrer un commentaire