Sql Group by 对于除一列外所有列都相同的行
我有一个包含许多列的表,将用于保存用户存储的搜索。
每天需要发送一封包含最新搜索结果的电子邮件。 可能有数千个已保存的搜索。
我保存的搜索表中的列类似于以下内容:
Id
userEmail
SearchParam1
SearchParam2
SearchParam3
由于会有很多搜索,我预计会有很多相同的搜索,因此我想获取 param1,2 和 3 相同但仍然有的每一行访问用户的电子邮件,这样我就可以向每个用户发送相同的结果,而不必多次运行相同的搜索。
理想情况下,我想要这样的东西:
SearchParam1, SearchParam2, SearchParam3
|
|____Email1
|
|____Email2
|
|____Email3
按搜索参数分组,但以某种方式链接到电子邮件。
我什至不知道从哪里开始,因此为什么我没有添加任何代码。 有人可以给我一些建议吗?
编辑 只是为了澄清。一封电子邮件不会在表中出现多次。因此从技术上讲,他们每人只能保存 1 次搜索。
没有这样的用户,只有电子邮件地址。
贝克斯
I have a table with a number of columns that will be used to save searches stored by a user.
Every day an email needs to be sent out containing the latest search results.
There could be thousands of saved searches.
My columns in my saved search table are similar to as follows:
Id
userEmail
SearchParam1
SearchParam2
SearchParam3
As there will be so many searches I anticipate there will be many that are the same so I would like to get every row where param1,2 and 3 are the same but still have access to the users email so I can send the same results to each without having to run the same search multiple times.
Ideally I'd like something like this back:
SearchParam1, SearchParam2, SearchParam3
|
|____Email1
|
|____Email2
|
|____Email3
Kind of grouped by the search params but linked to the emails some how.
I don't even know where to start, hence why I have not added any code.
Can anyone give me some suggestions?
EDIT
Just to clarify. One email would not exist more than once in the table. So technically they are only allowed to save 1 search each.
There are no users as such, just email addresses.
Bex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 SQL 中,起点是:(
如果您不想查看同一用户运行相同查询的次数,可以省略
group by
子句和count( *)
项,只需包含select unique
即可。)这将在每个用户的电子邮件旁边重复搜索参数。
如果您只想在每次更改其中之一时将搜索参数作为标题包含在内,我建议在报告工具(例如 SQLServer 报告服务)中使用上述查询 - 对搜索参数进行分组,在组中包含参数字段仅标题并仅在详细信息行中包含用户电子邮件。
编辑:如果 userEmail 唯一标识一条记录,则查询可以简化为:
In SQL, a starting point would be:
(If you don't want to see how many times the same user ran the same query, you can omit the
group by
clause and thecount(*)
item, and just includeselect distinct
instead.)This will repeat the search parameters next to each user's e-mail.
If you want to only include the search parameters as a heading each time one of them changes, I suggest using the above query in a reporting tool (such as SQLServer reporting services) - group on the search parameters, include the parameter fields in the group header only and include the user eMail in the detail line only.
EDIT: if userEmail uniquely identifies a record, the query can be simplified to:
您真正应该考虑的是数据标准化。这看起来像多对多关系,因此这样的结构是个好主意:
这样,如果您想获取订阅搜索参数的用户的所有电子邮件地址,那么这将是一个简单的查询:
这是使用多对多关系实现数据规范化的基本示例。它利用连接表来创建用户和搜索参数之间的关系。通过上述设计,数据检索变得更加简单,并且您不会受到原始非规范化数据(具有
searchParam1
、searchParam2
等列)的限制。请告诉我这是否有意义。
What you really should consider is data normalization. This looks like a many-to-many relationship, so a structure like this would be idea:
This way, if you wanted to get all the email addresses for users subscribed to a search parameters, it'd be a simple query:
This is a basic example of implementing data normalization with a many-to-many relationship. It utilizes a join table to create the relationship between users and search parameters. Data retrieval has become much simpler with the above design, and you aren't constricted by your original denormalized data (with columns like
searchParam1
,searchParam2
, etc.).Please let me know if that made sense.