0

下面是给定的表格。

----------------------------------
| area   |  country  |   date     |  
----------------------------------
| a1     |     c1    | 13-01-2013 | 
----------------------------------
| a2     |     c2    | 06-01-2013 | 
----------------------------------
| a3     |     c1    | 12-01-2013 | 
----------------------------------
| a4     |     c2    | 10-01-2013 | 
----------------------------------
| a5     |     c3    | 13-01-2013 | 
----------------------------------
| a1     |     c1    | 13-01-2013 | 
----------------------------------
| a2     |     c2    | 06-01-2013 | 
----------------------------------
| a3     |     c1    | 12-01-2013 | 
----------------------------------
| a4     |     c2    | 10-01-2013 | 
----------------------------------
| a5     |     c3    | 13-01-2013 | 

我希望我的查询能够跟随。

“显示在 2 个给定日期之间多次出现该地区的国家/地区的名称。”

我尝试了以下查询,但它给了我一个错误。> #1242 - 子查询返回多于 1 行

SELECT country
FROM table
WHERE area = (
SELECT area
FROM table
WHERE dateandtime > ' 13-01-2013'
AND dateandtime < '20-01-2013'
GROUP BY area
HAVING count(DISTINCT date) > 1 );
4

3 回答 3

1

要将值与值列表进行比较,您需要使用IN. 我还进行了一些更改以修复您的语法。

SELECT country
FROM table
WHERE area IN (
SELECT area
FROM table
WHERE dateandtime > ' 13-01-2013'
AND dateandtime < '20-01-2013'
GROUP BY area, country, `date`
HAVING count(date) > 1 )
);

我相信查询也可以简化为:

SELECT country
FROM table
GROUP BY area, country, `date`
HAVING count(date) > 1
AND dateandtime BETWEEN '14-01-2013' AND '19-01-2013'
);
于 2013-04-22T20:16:34.063 回答
0

如果你想要一个国家,不要选择一个地区:

SELECT DISTINCT country
FROM (
   SELECT country
   FROM table
   WHERE dateandtime > ' 13-01-2013'
   AND dateandtime < '20-01-2013'
   GROUP BY area
   HAVING count(DISTINCT date) > 1
) areas;
于 2013-04-22T20:16:56.000 回答
0

你可能会找这个

    SELECT country
    FROM table1

    WHERE date between ' 13-01-2013' AND  '20-01-2013'

    GROUP BY country
    HAVING count(date) > 1 ;

在这里演示

于 2013-04-22T20:24:15.840 回答