3

我有以下代码

select count(*)
from (select Annotations.user_id
from Annotations, Users
where Users.gender =  'Female'
and Users.user_id = Annotations.user_id
and image_id = 1
group by Annotations.user_id
having sum(case when stem = 'taxi' then 1 else 0 end) > 0 and
       sum(case when stem = 'zebra crossing' then 1 else 0 end) > 0
) Annotations

它会计算有多少女性给了图像 1 的茎“出租车”和“斑马线”。

样本数据

 user id, image id, stem
 1           1      image
 1           1      taxi
 1           1      zebra crossing
 2           1      person
 2           1      zebra crossing
 2           1      taxi
 3           1      person
 3           1      zebra crossing

预期结果(或类似)

stem1,  stem2,            count
taxi ,  zebra crossing      2
person, zebra crossing      2

但是,由于有 2000 多个词干,我无法全部指定。

我将如何使用 image_id = 1 和性别 = 女性而不是指定词干字符串来遍历词干行?

谢谢

4

3 回答 3

1

更新:据我了解,您想要选择 2 个词干的所有组合,并计算有多少用户拥有该词干组合。这是我的解决方案:

SELECT stem1, stem2, count(*) as count FROM
(
    SELECT a.user_id,a.image_id,a.stem as stem1,b.stem as stem2
    FROM Annotations a JOIN Annotations b
    ON a.user_id=b.user_id && b.image_id=a.image_id && a.stem!=b.stem
    JOIN Users ON Users.user_id = a.user_id
    WHERE Users.gender = "Female"
) as stems GROUP BY stem1, stem2 having count > 1 WHERE image_id=1;

这里需要注意的是,它将为每个词干组合返回 2 行。(第二次出现的词干顺序相反)。

于 2013-04-02T18:04:43.380 回答
1

据我了解,您需要获取拥有2 or more stems

更新:似乎您需要显示具有另一个用户使用的词干的用户,我已经更新了相同的查询

 SELECT
    distinct a.user_id,
    group_concat(DISTINCT a.stem ORDER BY a.stem)
 FROM
    Annotations a 
    JOIN Users u ON ( a.user_id = u.user_id AND u.gender =  'Female' )
    JOIN 
        (
            SELECT 
                b.user_id,
                b.stem
            FROM
                Annotations b
        ) AS b ON ( a.user_id <> b.user_id AND b.stem = a.stem )
 WHERE
    a.image_id = 1
GROUP BY
  a.user_id
于 2013-04-02T18:04:52.387 回答
0

这是我解决您的问题的尝试:

SELECT COUNT(*) AS Count, a1.stem AS Stem1, a2.Stem AS Stem2 
FROM Annotations AS a1
INNER JOIN Annotations AS a2 ON a1.user_id = a2.user_id AND a1.image_id = a2.image_id
                          AND a1.stem < a2.stem 
WHERE a1.image_id = 1
GROUP BY a1.stem, a2.Stem
HAVING COUNT(*) > 1; 

我没有包括image_id逻辑。

请在此处查看我的 SQL Fiddle: http ://sqlfiddle.com/#!2/4ee69/33

根据以下数据(从您的数据中复制),我得到了发布在其下方的结果。

CREATE TABLE Annotations
    (`user_id` int, `image_id` int, `stem` varchar(14))
;

INSERT INTO Annotations
    (`user_id`, `image_id`, `stem`)
VALUES
    (1, 1, 'image'),
    (1, 1, 'taxi'),
    (1, 1, 'zebra crossing'),
    (2, 1, 'person'),
    (2, 1, 'zebra crossing'),
    (2, 1, 'taxi'),
    (3, 1, 'person'),
    (3, 1, 'zebra crossing')
;

  COUNT STEM1   STEM2
    2   person  zebra crossing
    2   taxi    zebra crossing
于 2013-04-02T20:08:55.280 回答