求一个sql语句,关于统计的

yufulou 2010-12-23 07:41:01
两个表
网站访问表:用户,请求次数,站点id,访问时间(每1小时相应用户、站点有一次记录)
网站与网站类型关系表:站点id,网站类型id (两个id之间为n对n的关系)

某个用户访问了多少种不同的站点,不同的站点类型,以及总共有多少次请求(就这里犯难了)
查询条件有:限制时间段,限制网站类型id在某个范围内
分组条件是:根据用户分组,到最后应该是每个用户对应了多少种不同的站点,不同的站点类型,不同的请求数

曾经用过的方法:
SELECT count(distinct 站点id), count(distinct 网站类型id), sum(请求次数) from 网站访问表,关系表 where 网站访问表.站点id=关系表.站点id AND 关系表.网站类型id in (范围) AND 请求时间;

这个结果就是 站点数,网站类型数都对,但是请求次数那错了,由于cross join,一个站点对应多个站点类型,而产生了重复的数据

请教高人指教我应该怎么在这一条语句中再拿到一个用户在这个时段内正确的请求次数,谢谢!
...全文
93 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
yufulou 2010-12-31
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 acmain_chm 的回复:]

引用这个只能在m_requests都不相同的时候才可以,如果有相同的,就会被归并的……学会举例,否则别人怎么去猜啊。
[/Quote]
呵呵,谢谢啦~~
ACMAIN_CHM 2010-12-24
  • 打赏
  • 举报
回复
[Quote]这个只能在m_requests都不相同的时候才可以,如果有相同的,就会被归并的……[/Quote]学会举例,否则别人怎么去猜啊。
yufulou 2010-12-24
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wwwwa 的回复:]

SELECT a.user_id,SUM(DISTINCT a.m_requests ),COUNT(*)
FROM table_log_site a LEFT JOIN table_sites_type_urls b ON a.site_id=b.site_id
where find_in_set(given_sites_type_str,sites_type_urls)>0
GROUP……
[/Quote]
这个只能在m_requests都不相同的时候才可以,如果有相同的,就会被归并的……
WWWWA 2010-12-24
  • 打赏
  • 举报
回复
SELECT a.user_id,SUM(DISTINCT a.m_requests ),COUNT(*)
FROM table_log_site a LEFT JOIN table_sites_type_urls b ON a.site_id=b.site_id
where find_in_set(given_sites_type_str,sites_type_urls)>0
GROUP BY a.user_id
yufulou 2010-12-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wwwwb 的回复:]

SELECT a.user_id,SUM(DISTINCT a.m_requests ),COUNT(*)
FROM table_log_site a LEFT JOIN table_sites_type_urls b ON a.site_id=b.site_id
GROUP BY a.user_id
[/Quote]

这个只能在m_requests都不相同的时候才可以,如果有相同的,就会被归并的
yufulou 2010-12-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 acmain_chm 的回复:]

SQL code
mysql> select * from table_log_site;
+--------+------------+-----------+---------+---------+
| m_date | m_requests | m_accepts | site_id | user_id |
+--------+------------+-----------+-----……
[/Quote]

哎,反正总觉得这个查询效率很慢啊…………
yufulou 2010-12-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 acmain_chm 的回复:]

SQL code
mysql> select * from table_log_site;
+--------+------------+-----------+---------+---------+
| m_date | m_requests | m_accepts | site_id | user_id |
+--------+------------+-----------+-----……
[/Quote]

呵呵,谢谢~~

那如果我想通过sites_type_id(一个用“,”隔开的字符串,比如就用变量given_sites_type_str)来限制site_id,那我应该在后面再加个EXISTS (SELECT site_id FROM table_sites_type_urls WHERE site_id=t.site_id AND sites_type_urls IN(given_sites_type_str) limit 1)吧?通过 prepare statement from str的方式
wwwwb 2010-12-24
  • 打赏
  • 举报
回复
SELECT a.user_id,SUM(DISTINCT a.m_requests ),COUNT(*)
FROM table_log_site a LEFT JOIN table_sites_type_urls b ON a.site_id=b.site_id
GROUP BY a.user_id
ACMAIN_CHM 2010-12-24
  • 打赏
  • 举报
回复
mysql> select * from table_log_site;
+--------+------------+-----------+---------+---------+
| m_date | m_requests | m_accepts | site_id | user_id |
+--------+------------+-----------+---------+---------+
| 123456 | 4 | 3 | 1 | 1 |
| 123456 | 5 | 4 | 2 | 1 |
| 123456 | 6 | 5 | 1 | 2 |
| 123456 | 7 | 6 | 2 | 2 |
+--------+------------+-----------+---------+---------+
4 rows in set (0.00 sec)

mysql> select * from table_sites_type_urls;
+---------+---------------+
| site_id | sites_type_id |
+---------+---------------+
| 1 | 2 |
| 1 | 3 |
| 2 | 4 |
| 2 | 5 |
+---------+---------------+
4 rows in set (0.00 sec)

mysql> select user_id,sum(m_requests) as total_request,
-> (select count(*)
-> from table_sites_type_urls a inner join table_log_site b using(site_id)
-> where b.user_id=t.user_id
-> ) as total_sites_type
-> from table_log_site t
-> group by user_id;
+---------+---------------+------------------+
| user_id | total_request | total_sites_type |
+---------+---------------+------------------+
| 1 | 9 | 4 |
| 2 | 13 | 4 |
+---------+---------------+------------------+
2 rows in set (0.00 sec)

mysql>
yufulou 2010-12-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 acmain_chm 的回复:]

(不要高估你的汉语表达能力或者我的汉语理解能力)
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html

1. 你的 create table xxx .. 语……
[/Quote]

哦,抱歉~~
DROP TABLE IF EXISTS table_log_site;
CREATE TABLE table_log_site(
m_date INTEGER,
m_requests INTEGER,
m_accepts INTEGER,
site_id INTEGER,
user_id INTEGER
);

DROP TABLE IF EXISTS table_sites_type_urls;
CREATE TABLE table_sites_type_urls(
site_id INTEGER,
sites_type_id INTEGER
);

INSERT INTO table_log_site(m_date, m_requests, m_accepts, site_id, user_id)
VALUES
(123456, 4, 3, 1, 1),
(123456, 5, 4, 2, 1),
(123456, 6, 5, 1, 2),
(123456, 7, 6, 2, 2);
INSERT INTO table_sites_type_urls(site_id, sites_type_id)
VALUES
(1, 2),
(1, 3),
(2, 4),
(2, 5);

select count(distinct table_sites_type_urls.sites_type_id) as total_sites_type,sum(table_log_site.m_requests) as total_request , user_id from table_log_site,table_sites_type_urls where table_log_site.site_id=table_sites_type_urls.site_id group by table_log_site.user_id;

+------------------+---------------+---------+
| total_sites_type | total_request | user_id |
+------------------+---------------+---------+
| 4 | 18 | 1 |
| 4 | 26 | 2 |
+------------------+---------------+---------+

就是这个语句中的total_request由于cross join出了问题。我希望能得到一个9,一个13
yufulou 2010-12-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yufulou 的回复:]

哦,抱歉~~
DROP TABLE IF EXISTS table_log_site;
CREATE TABLE table_log_site(
m_date INTEGER,
m_requests INTEGER,
m_accepts INTEGER,
site_id INTEGER,
user_id INTEGER
);

DROP TABLE IF EXISTS tab……
[/Quote]

哦,抱歉~~
DROP TABLE IF EXISTS table_log_site;
CREATE TABLE table_log_site(
m_date INTEGER,
m_requests INTEGER,
m_accepts INTEGER,
site_id INTEGER,
user_id INTEGER
);

DROP TABLE IF EXISTS table_sites_type_urls;
CREATE TABLE table_sites_type_urls(
site_id INTEGER,
sites_type_id INTEGER
);

INSERT INTO table_log_site(m_date, m_requests, m_accepts, site_id, user_id)
VALUES
(123456, 4, 3, 1, 1),
(123456, 5, 4, 2, 1),
(123456, 6, 5, 1, 2),
(123456, 7, 6, 2, 2);
INSERT INTO table_sites_type_urls(site_id, sites_type_id)
VALUES
(1, 2),
(1, 3),
(2, 4),
(2, 5);

select count(distinct table_sites_type_urls.sites_type_id) as total_sites_type,sum(table_log_site.m_requests) as total_request , user_id from table_log_site,table_sites_type_urls where table_log_site.site_id=table_sites_type_urls.site_id group by table_log_site.user_id;

+------------------+---------------+---------+
| total_sites_type | total_request | user_id |
+------------------+---------------+---------+
| 4 | 18 | 1 |
| 4 | 26 | 2 |
+------------------+---------------+---------+

就是这个语句中的total_request由于cross join出了问题。我希望能得到一个9,一个13
yufulou 2010-12-24
  • 打赏
  • 举报
回复
哦,抱歉~~
DROP TABLE IF EXISTS table_log_site;
CREATE TABLE table_log_site(
m_date INTEGER,
m_requests INTEGER,
m_accepts INTEGER,
site_id INTEGER,
user_id INTEGER
);

DROP TABLE IF EXISTS table_sites_type_urls;
CREATE TABLE table_sites_type_urls(
site_id INTEGER,
sites_type_id INTEGER
);

INSERT INTO table_log_site(m_date, m_requests, m_accepts, site_id, user_id)
VALUES
(123456, 4, 3, 1, 1),
(123456, 5, 4, 2, 1),
(123456, 6, 5, 1, 2),
(123456, 7, 6, 2, 2);
INSERT INTO table_sites_type_urls(site_id, sites_type_id)
VALUES
(1, 2),
(1, 3),
(2, 4),
(2, 5);

select count(distinct table_sites_type_urls.sites_type_id) as total_sites_type,sum(table_log_site.m_requests) as total_request , user_id from table_log_site,table_sites_type_urls where table_log_site.site_id=table_sites_type_urls.site_id group by table_log_site.user_id;

+------------------+---------------+---------+
| total_sites_type | total_request | user_id |
+------------------+---------------+---------+
| 4 | 18 | 1 |
| 4 | 26 | 2 |
+------------------+---------------+---------+

就是这个语句中的total_request由于cross join出了问题。我希望能得到一个9,一个13
WWWWA 2010-12-23
  • 打赏
  • 举报
回复
贴记录及要求结果出来看看
ACMAIN_CHM 2010-12-23
  • 打赏
  • 举报
回复
(不要高估你的汉语表达能力或者我的汉语理解能力)
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html

1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)

这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。

yufulou 2010-12-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wwwwa 的回复:]

贴记录及要求结果出来看看
[/Quote]
上面的查询语句稍微改一下:
SELECT 用户,count(distinct 站点id), count(distinct 网站类型id), sum(请求次数) from 网站访问表,关系表 where 网站访问表.站点id=关系表.站点id AND 关系表.网站类型id in (范围) AND 请求时间
GROUP BY 用户

网站访问表:
用户,请求次数,站点id,访问时间
用户1 5 3 12345
网站关系表:
站点id,网站类型id
3 1
3 2
查找结果为:
用户,站点数,类型数,总请求数
用户1 1 2 10

就是总请求数这里出问题了……

56,678

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧