进行“全球”合作的最佳方式GROUP BY 存储为“field1”、“field2”的字段
我所拥有的是一个具有不同字段的表,其中的字段称为类别1、类别2。假设那里存储了一些数据,
(cat1, cat2), (cat2, cat1), (cat3, cat1), (cat3, cat4), (cat1, cat2), (cat2, cat3)
一行不能有重复的类别,即(cat1,cat1)是不可能的。有没有一种简单的方法可以获得这样的输出
(cat1, 4), (cat2, 4), (cat3, 3), (cat4, 1)
我目前所做的是
(SELECT category1 AS category UNION ALL SELECT category2 AS category) AS tmp
然后处理这个 tmp 表并按它分组。问题是我找不到优化它的方法。所以问题是:你能想出更好的方法来执行上面的选择,从而允许我使用索引吗?如果没有,唯一的方法是执行此操作 - 实际创建此临时表并为其建立索引吗?
What I have is a table that has different fields and among them the fields that are called category1, category2. Let's say there is some data stored there
(cat1, cat2), (cat2, cat1), (cat3, cat1), (cat3, cat4), (cat1, cat2), (cat2, cat3)
A row can't have duplicate categories, i.e. (cat1, cat1) is not possible. Is there an easy way to get an output like this
(cat1, 4), (cat2, 4), (cat3, 3), (cat4, 1)
What I currently do is
(SELECT category1 AS category UNION ALL SELECT category2 AS category) AS tmp
and then I deal with this tmp table and group by it. The problem is that I can't find a way to optimize it. So the question is: can you think of any better way to do the select above which would allow me to use indexes? If not, is the only way to do this - to actually create this tmp table and index it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)