在MYSQL中计算具有特殊条件的重复条目
我对 MYSQL 并不陌生,但我无法解决这个问题。非常感谢任何帮助!
我的表格针对以下示例进行了简化:
categories_table +-----+ | id | +-----+ | 1 | | 2 | | 3 | +-----+
links_table +------+-------------+-------------------+ | id | category_id | link | +------+-------------+-------------------+ | 1 | 1 | http://google.com | | 2 | 2 | http://google.com | | 3 | 2 | http://google.com | | 4 | 5 | http://yahoo.com | | 5 | 1 | http://php.net | | 6 | 2 | http://php.net | +------+-------------+-------------------+
在链接表中,我有数千行(是的,连接到同一类别的相同链接),并且我希望有一个查看列出 DISTINCT 链接的表,以及链接附加到的 DISTINCT 类别的总和。所以在 links_table 中 id 的 2 和 3 应该只算一次!
因此,在此示例中,视图表应如下所示:
view +-------------------+-------------------+ | sum_of_categories | link | +-------------------+-------------------+ | 2 | http://google.com | | 1 | http://yahoo.com | | 2 | http://php.net | +-------------------+-------------------+
有人可以帮忙吗?
I am not new to MYSQL but I can't get my head around this problem. Any help is greatly appreciated!
My tables are simplified for the example:
categories_table +-----+ | id | +-----+ | 1 | | 2 | | 3 | +-----+
links_table +------+-------------+-------------------+ | id | category_id | link | +------+-------------+-------------------+ | 1 | 1 | http://google.com | | 2 | 2 | http://google.com | | 3 | 2 | http://google.com | | 4 | 5 | http://yahoo.com | | 5 | 1 | http://php.net | | 6 | 2 | http://php.net | +------+-------------+-------------------+
In links table I have thousands of rows (yes, the same links connected to the same category), and I would like to have a view table that lists the DISTINCT links, with the sum of DISTINCT categories a link is attached to. So in links_table id's 2 and 3 should only count once!
So in this example, the view table should look like this:
view +-------------------+-------------------+ | sum_of_categories | link | +-------------------+-------------------+ | 2 | http://google.com | | 1 | http://yahoo.com | | 2 | http://php.net | +-------------------+-------------------+
Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)