Multiple value on between query [PostgreSQL]
i have a simple query like this
select * from user where xxx between a and b
but how if i have multiple input, like xxx, yyy, mmm, bbb.
with same sintax,
select * from user where xxx,yyy,mmm,bbb between a and b
i want select all user that a and b is beetween xxx,yy开发者_如何学Pythony,mmm and bbb. so it will return all user that a and b is match with between creteria.
Thanks
SELECT *
FROM user
WHERE (xxx BETWEEN a and b)
AND (yyy BETWEEN a and b)
AND (mmm BETWEEN a and b)
AND (bbb BETWEEN a and b)
If you want all criteria to match. If you want atleast one of them to match, use OR
instead of AND
.
I would create two extra tables to store your dates :
date_type (date_type_id, date_label)
user_date (user_id, date_type_id, date_value)
Which could be populated that way :
date_type (1, 'Sign in')
date_type (2, 'Sign out')
user_date (1, 1, '2010-08-11 02:14:20')
user_date (1, 1, '2010-08-11 02:36:54')
// etc
You would then have to do some JOIN to achieve what you want :
SELECT *
FROM user
INNER JOIN user_date
USING (user_id)
INNER JOIN date_type
USING (date_type_id)
WHERE date_value BETWEEN a AND b;
AND date_type_id IN (1,2) -- if necessary, you can choose which
-- types to check
精彩评论