我有一个名为words
的表格,由三列word(VARCHAR(16)), doc_id(INT), weight(DOUBLE)
组成。
这是我需要做的,我有两个问题:
SELECT doc_id, weight FROM words WHERE word = 'bla';
doc_id weight
------ ------
1 0.14
2 0.61
3 0.32
和
SELECT doc_id, weight FROM words WHERE word = 'blabla';
doc_id weight
------ ------
2 0.19
3 0.45
4 0.14
我需要在doc_id
上获取两者的交集,并选择较低的weight
值作为权重,即我希望结果为:
doc_id weight
------ ------
2 0.19
3 0.32
有没有办法在单个查询中执行此操作?在程序中这样做会让它变慢!
我还需要获取UNION
并选择较高的weight
值,即我希望结果为:
doc_id weight
------ ------
1 0.14
2 0.61
3 0.45
4 0.14
请注意,word
和doc_id
列不是唯一的,因此可以将一个字分配给多个文档。
答案 0 :(得分:1)
对于交叉部分,听起来你想要“所有doc_id的最低权重,其中doc_id对于单词'bla'有一行而对于'blabla'这个单词有一行”。
可以找到(untested)
select w1.doc_id, least(min(w1.weight), min(w2.weight)) as minweight
from words w1, words w2
where w1.doc_id = w2.doc_id
and w1.word = 'bla'
and w2.word = 'blabla'
group by w1.doc_id;
对于联合部分,你想要的是“所有doc_id的最高权重,其中doc_id有一行'bla'或一行'blabla'”。
可以找到(untested)
select doc_id, max(weight) as maxWeight
from words
where word in ('bla', 'blabla')
group by doc_id;
答案 1 :(得分:1)
select w1.doc_id
, least(w1.weight, w2.weight) weight
from words w1
inner
join words w2
on w1.doc_id = w2.doc_id
where w1.word = 'bla1'
and w2.word = 'bla2'
select doc_id
, max(weight) weight
from words
where word in ('blah1', 'blah2')
group
by doc_id
答案 2 :(得分:0)
我认为你想要这个:
SELECT doc_id, MIN(weight) MinWeight, MAX(weight) MaxWeight
FROM words
WHERE word IN ('bla','blabla')
GROUP BY doc_id
答案 3 :(得分:0)
路口:
SELECT doc_id, MIN(weight) as MinWeight FROM words
WHERE doc_id IN
(SELECT doc_id FROM words WHERE word = 'bla')
AND doc_id IN
(SELECT doc_id FROM words WHERE word = 'blabla')
GROUP BY doc_id
联:
SELECT doc_id, MAX(weight) as MaxWeight FROM words
WHERE word IN ('bla', 'blabla')
GROUP BY doc_id
答案 4 :(得分:0)
根据我的理解,以下查询应该适用于您的要求。
- 降低重量查询 选择Z.doc_id,MIN(权重)为LOWER_WEIGHT 从 ( SELECT doc_id,weight FROM words WHERE word ='bla' 相交 SELECT doc_id,weight FROM words WHERE word ='blabla' )z GROUP BY Z.DOC_ID;
- 更高权重查询
选择Z.doc_id,MAX(权重)为HIGHER_WEIGHT 从 ( SELECT doc_id,weight FROM words WHERE word ='bla' 联盟 SELECT doc_id,weight FROM words WHERE word ='blabla' )z GROUP BY Z.DOC_ID;
此致 Venk