任何人都可以帮我修改以下SQL查询。
select pid from activities_2009_1 where purpose IN(1,2);
在此查询中,我有目的地使用IN运算符。这给了我所有有目的= 1或目的2的pids但是,我想选择所有目的= 1和目的2的pid,
PID A P S D
612747617 0 1 0 27300
612747617 1 2 28740 29280
612747617 2 1 59700 60
612747617 3 4 61200 120
612747617 4 1 62700 37300
612747631 0 1 0 36000
612747631 1 4 36600 2100
612747631 2 4 43200 56800
vt_activities_2009_1表中的示例数据如上所示。
其中A表示anum,P表示目的等。
我无法在目的= 1和目的= 2的情况下使用。
我将非常感谢你的帮助。
答案 0 :(得分:1)
怎么样:
select pid
from your_table
where purpose=1
intersect
select pid
from your_table
where purpose=2;
我认为应该这样做。
实际上只是想到了这一点,它将在一次通过表格中完成:
select pid
from (
select pid
from my table
where purpose in(1,2)
)
group by pid
having count(*) = 2;
这应该是一次通过表格。
答案 1 :(得分:0)
SELECT FIPS, count(pid) from
(SELECT distinct sa.*
from(
SELECT p.pid, concat(lpad(h.state, 2, '0'), lpad(h.county,3,'0')) FIPS
FROM PERSON_2009_1 p,HOUSEHOLD_2009_1 h
WHERE p.schg = 4
AND p.agep BETWEEN 10 AND 25
and p.hid = h.hid) sa
where sa.pid in (select pid from activities_2009_1 x where exists(select 1 from activities_2009_1 y where x.pid = y.pid and purpose =1)
and exists(select 1 from activities_2009_1 y where x.pid = y.pid and purpose =2)
)group by FIPS order by FIPS;
答案 2 :(得分:0)
SELECT FIPS, count(pid) from(SELECT distinct sa.* from(SELECT p.pid, concat(lpad(h.state, 2, '0'), lpad(h.county,3,'0')) FIPS
FROM PERSON_2009_1 p,HOUSEHOLD_2009_1 h
WHERE p.schg = 4 AND p.agep BETWEEN 10 AND 25 and p.hid = h.hid)
sa where sa.pid in ((select pid from activities_2009_1 where purpose = 1) as a inner join (select pid from activities_2009_1 where purpose = 2) as b on (a.pid = b.pid))
)group by FIPS order by FIPS;
我正在使用内连接(交叉)。不是最有效的,但应该有效。
以下是我第一次想到使用AND:
SELECT FIPS, count(pid) from(SELECT distinct sa.* from(SELECT p.pid, concat(lpad(h.state, 2, '0'), lpad(h.county,3,'0')) FIPS
FROM PERSON_2009_1 p,HOUSEHOLD_2009_1 h
WHERE p.schg = 4 AND p.agep BETWEEN 10 AND 25 and p.hid = h.hid)
sa where sa.pid in ((select pid from activities_2009_1 where purpose = 1) ANDas sa.pida ininner join (select pid from activities_2009_1 where purpose = 2) as b on (a.pid = b.pid))
)group by FIPS order by FIPS;
答案 3 :(得分:0)
我想你可能想要这个:
SELECT FIPS, count(pid) from
( SELECT distinct sa.* from
(SELECT p.pid, concat(lpad(h.state, 2, '0'), lpad(h.county,3,'0')) FIPS
FROM PERSON_2009_1 p,HOUSEHOLD_2009_1 h
WHERE p.schg = 4
AND p.agep BETWEEN 10 AND 25
and p.hid = h.hid
) sa
where sa.pid in (select pid from activities_2009_1 where purpose = 1)
AND sa.pid in (select pid from activities_2009_1 where purpose = 2)
)
group by FIPS order by FIPS;
或者:
SELECT FIPS, count(pid) from
( SELECT distinct sa.* from
(SELECT p.pid, concat(lpad(h.state, 2, '0'), lpad(h.county,3,'0')) FIPS
FROM PERSON_2009_1 p,HOUSEHOLD_2009_1 h
WHERE p.schg = 4
AND p.agep BETWEEN 10 AND 25
and p.hid = h.hid
) sa
where 2 = (select count(distinct a.purpose)
from activities_2009_1 a
where a.pid = sa.pid
and a.purpose in (1,2))
)
group by FIPS order by FIPS;
答案 4 :(得分:0)
这对你来说很有用......
SELECT PID
, A
, P
, S
, D
FROM ACTIVITIES_2009_1
WHERE PID IN (SELECT PID
FROM ( SELECT PID, COUNT (*)
FROM ACTIVITIES_2009_1
WHERE P = 1
OR P = 2
GROUP BY PID
HAVING COUNT (*) > 1))
答案 5 :(得分:0)
尝试为表a
和b
创建作业。 a
将验证目的是否存在= 1,b
将验证目的是否存在= 2。如果缺少任何一个目的,inner join
将删除该行。
select a.pid
from activities_2009_1 a
inner join activities_2009_1 b
on a.pid=b.pid
where a.purpose=1 and b.purpose=2;
答案 6 :(得分:0)
我们如何谈论select
的含义,而不是给出你眼前问题的答案。
简单形式select <columns> from <table> where <conditions>
几乎是自我解释。选择(返回)的是表中条件为真的行。为了实现你想要的条件(“对于同一个pid有一个p = 1和p = 2”),你首先必须确保两个p列在同一行。让我们再说一遍。为了实现你想要的条件(“对于同一个pid有一个p = 1和p = 2”),你首先必须确保两个p列在同一行。
这就是联接可以为您做的事情:将两个表中的行对齐以创建一个包含两个值的新表。在这种情况下,pid上的连接(对齐),以及对left.p = 1和right.p = 2的检查实现了这一点。我确定在其中一个答案中有一个例子(l和r指的是加入的两个表格(“左”和“右”),在这种情况下是同一个表两次。
使用count()=2
的示例非常聪明且(可能)有效但依赖于每个pid + p组合唯一的假设(或约束)。
至于你的(连接)查询的效率,我建议你在关闭sql语法之后开始关心它(以及索引,规范化/非规范化数据库设计和约束)。 “高效运行”查询所获得的时间可能不会超过开发人员效率低下/由于代码不清晰而导致的时间损失。硬件比人工软件更容易扩展。