简体   繁体   English

使用 SQL 查找访问过 URL A 和 B 的用户?

[英]Find users who have visited URL A and B with SQL?

Let's say you have a table with user, url, datetime , in which each row is a website visit.假设您有一个包含user, url, datetime的表,其中每一行都是一次网站访问。

How to find users who have visited both an URL contaning string pattern A and visited an URL containing string pattern B?如何找到既访问过包含字符串模式 A 的 URL 又访问包含字符串模式 B 的 URL 的用户?

The fact it's "containing a string pattern ...", and not a simple equality makes it impossible to use a query with something like事实上它“包含一个字符串模式......”,而不是一个简单的相等性使得不可能使用类似的查询

url in ('action1.php', 'action2.php')

like in SQL querying a customer ID who ordered both product A and B .就像在SQL 中查询同时订购产品 A 和 B 的客户 ID一样。

You can use group by and having :您可以使用group byhaving

select user
from t
where url like '%a%' or
      url like '%b%'
group by user
having sum(url like '%a%') > 0 and
       sum(url like '%b%') > 0;

If you don't want to repeat the comparisons, you can leave out the where clause or use:如果不想重复比较,可以省略where子句或使用:

select user
from (select t.*, (url like '%a%') as has_a, (url like '%n%') as has_b
      from t
     ) t
where has_a or has_b
group by user
having sum(has_a) > 0 and
       sum(has_b) > 0;

Assuming "/testing" and "/staging" are the two URL patterns.假设“/testing”和“/staging”是两种 URL 模式。 You can use this你可以用这个

SELECT   user
FROM     `table`
WHERE    url LIKE '%/testing%' or
         url LIKE '%/staging%'
GROUP BY user
HAVING   (COUNT(url LIKE '%/testing%') > 0 and COUNT(url LIKE '%/staging%') > 0)

If you need more info on pattern matching you can do a search on "pattern matching SQL" and "SQL regular expression".如果您需要有关模式匹配的更多信息,您可以搜索“模式匹配 SQL”和“SQL 正则表达式”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找每个站点访问过至少两个不同国家的唯一身份用户数 - Find the number of unique users who have visited at least two different countries per site SQL:返回访问过同一地点的用户的列表 - SQL: Return a list of users who visited the same place sql - 查找连续执行 3 次操作的用户 - sql - find users who made 3 consecutive actions Rails/SQL - 如何找到没有高级订阅的用户加入 - Rails/SQL - How to find users who does not have premium subscription with joins 如何找到具有sysadmin访问SQL Server Express权限的Windows用户和组? - How do I find Windows users and groups who have sysadmin access to SQL Server Express? SQL 查询在被检查的瞬间之前访问该站点超过2次的userCount - SQL Query to get userCount who have visited the site more than 2 times before the instant when it is checked SQL查询,过去一年访问过​​的人 - SQL query, who visited in the past year 找出没有特定角色的用户 - Find out users who do not have a particular role 如何设计一个SQL查询,该查询将向我显示24小时内最近20天内至少访问了一页的所有用户? - How do I design a SQL query that will show me all users who visited at least one page for the last 20 out of 24 hours? 查询SQL中所有看过相同电影的用户 - Query all users who have watched the same movies in SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM