我想获取在x天和y天下订单的工人的名字。
select name
from worker,
orders
where worker.workerid = oders.workerid
and date = x and date = y;
所以我只想让两天都下订单的工人。
答案 0 :(得分:2)
in (x,y)
将为您提供在至少两天内工作的工人。
尽管这里的工作人员将为那些工作了2天的人提供帮助。
group by name
having count(distinct date)=2
总而言之,您得到了想要的东西
select name
from worker,
orders
where worker.workerid = oders.workerid
and date in (x,y)
group by name
having count(distinct date)=2
答案 1 :(得分:1)
执行GROUP BY
。使用HAVING
确保两个日期都在那里。
select name
from worker
join orders
on worker.workerid = orders.workerid
where date in (x, y)
group by name
having count(distinct date) >= 2
请注意切换到现代的显式JOIN
语法!
答案 2 :(得分:1)
使用两个where子句...
SELECT name
FROM worker AS w
WHERE EXISTS (SELECT 1
FROM orders AS o
WHERE w.workerid = o.workerid
AND date = x)
AND EXISTS (SELECT 1
FROM orders AS o
WHERE w.workerid = o.workerid
AND date = y)
答案 3 :(得分:1)
最简单的方法是使用2个部分查询和postgre Intersect
运算符:
select name
from worker,
orders
where worker.workerid = oders.workerid
and date = y
INTERSECT
select name
from worker,
orders
where worker.workerid = oders.workerid
and date = x;
这将仅保留在X
和y
处于活动状态的工作人员名称。
http://www.postgresqltutorial.com/postgresql-intersect/
类似于UNION和EXCEPT运算符,PostgreSQL INTERSECT运算符 将两个或多个SELECT语句的结果集合并为一个 单个结果集。 INTERSECT运算子会传回 在两个结果集中都可用,或两个查询都返回。
答案 4 :(得分:-1)
尝试一下:
SELECT name from worker, orders
WHERE worker.workerid = oders.workerid
DATE date IN (x, y)