【发布时间】:2014-04-01 10:15:11
【问题描述】:
DBMS 是我正在使用 Mysql。我有这些表:
路径表:
id idSentence idPath token isTV idC
1 s0001 p1 test1 true ic000041
2 s0001 p1 test2 true ic000041
3 s0002 p2 test3 true ic000042
4 s0002 p3 test4 false ic000042
5 s0002 p3 test5 true ic000042
6 s0002 p4 test6 false ic000042
7 s0002 p4 test7 true ic000042
8 s0002 p4 test8 true ic000042
9 s0002 p4 test9 false ic000042
10 s0003 p5 test10 false ic000044
11 s0003 p5 test11 false ic000044
12 s0003 p5 test12 false ic000044
13 s0003 p6 test13 false ic000044
14 s0003 p6 test14 true ic000044
关系表:
id id2 rel
3 4 nsubj
4 5 dobj
6 7 pobj
8 9 nsubjpass
10 11 pobj
目录:
idC tag
ic000040 a
ic000041 p
ic000042 div
ic000043 b
ic000044 i
我想为每个 idSentence(路径表)创建一个查询,我使用以下条件选择每个带有 idSentence、idPath、tokens、isTV、rel 和标签的元组:
- 我只想选择在 isTV 中至少包含一个值“true”的 idPath
- 我只想选择 2 到 3 之间的 idPath 的不同长度(例如,对于路径表,我得到以下长度:1、3 和 2,使用以下查询:select count(distinct(idPath)) as cc来自 idSentence 的路径组;所以在这种情况下,我只想得到 3 和 2);
- 我想在 2 到 3 之间选择每个 idPath 的长度(例如,使用查询:select count(*) as cc from path group by idPath,我得到:2,1,2,4,3, 2 我将只选择值:2,2,3,2)
我创建了这个查询:
SELECT p.idSentence, p.idPath, p.token, p.isTV, r.rel, t.tag
FROM path p LEFT OUTER JOIN relation r
ON (p.id = r.id) JOIN content t ON(p.idC=t.idC)
JOIN
(select idPath, max(case when p.isTV = 'true' then 1 else 0 end) as HasTv,
(case when COUNT(*) between 2 and 3 then 1 else 0 end) as Has
from path p
group by idPath
) pf
on p.idPath = pf.idPath and
pf.HasTv = 1 and pf.Has = 1;
但我也应该在连接条件中添加中心:
select count(distinct(idPath)) as cc from path group by idSentence
如何修改查询以添加此条件?
我已经添加了条件:
select count(*) as cc from path group by idPath
通过以下声明:
(case when COUNT(*) between 2 and 3 then 1 else 0 end) as Has
我对查询进行了测试,并且部分可以正常工作:QUERY
【问题讨论】: