我有以下表格:
create temporary table Items (item_id int, item_name varchar(10));
create temporary table ItemRating (item_id int, rating int);
具有以下数据:
insert into Items (item_id, item_name) values (1,'Item 1'),(2,'Item 2'),(3,'Item 3'),(4,'Item 4'),(5,'Item 5');
insert into ItemRating values (1,9),(1,6),(3,10);
然后我运行以下查询:
select i.item_id, i.item_name, avg(ir.rating) from Items i left join ItemRating ir ON ir.item_id = i.item_id group by ir.item_id;
这是我得到的结果:
+---------+-----------+----------------+
| item_id | item_name | avg(ir.rating) |
+---------+-----------+----------------+
| 2 | Item 2 | NULL |
| 1 | Item 1 | 7.5000 |
| 3 | Item 3 | 10.0000 |
+---------+-----------+----------------+
现在,我完全明白查询写错了,我想要的是对 i.item_id 进行分组。但我不明白这种行为。为什么MYSQL在结果中显示item_id为2,而不是4或5?我实际上希望只看到项目 1 和 3,因为它们是唯一在 ItemRating 中具有相应记录的项目。
那么,谁能给我解释一下 MYSQL 在这里做什么?
最佳答案
这是正在发生的事情。逐个考虑查询以及 MySQL 正在处理的内容。
首先,您要从项目中进行选择(从项目 i 中选择 i.item_id、i.item_name、avg(ir.rating)):
+---------+-----------+
| item_id | item_name |
+---------+-----------+
| 1 | Item 1 |
| 2 | Item 2 |
| 3 | Item 3 |
| 4 | Item 4 |
| 5 | Item 5 |
+---------+-----------+
然后您将加入评级(left join ItemRating ir ON ir.item_id = i.item_id)。请注意,Item 1 在连接后出现在两行中,因为这就是 JOIN 定义的工作方式——它为每个连接条件匹配返回一行(并且 LEFT 基本上意味着“至少返回第一个表中的每一行,即使该行没有连接条件匹配”。
+---------+-----------+-----------+------------+
| item_id | item_name | ir.rating | ir.item_id |
+---------+-----------+-----------+------------+
| 1 | Item 1 | 9 | 1 |
| 1 | Item 1 | 6 | 1 |
| 2 | Item 2 | NULL | NULL |
| 3 | Item 3 | 10 | 3 |
| 4 | Item 4 | NULL | NULL |
| 5 | Item 5 | NULL | NULL |
+---------+-----------+-----------+------------+
最后,您按评级分组(group by ir.item_id)。这将为每个唯一的 ir.item_id 返回一行。有三个唯一的 ir.item_id(如您在最后一列中所见):1、NULL 和 3。对于其中的每一个,它返回一行并对评分进行平均。
因此,对于 1,我们有:
+---------+-----------+-----------+------------+
| item_id | item_name | ir.rating | ir.item_id |
+---------+-----------+-----------+------------+
| 1 | Item 1 | 9 | 1 |
| 1 | Item 1 | 6 | 1 |
+---------+-----------+-----------+------------+
折叠成:
+---------+-----------+----------------+------------+
| item_id | item_name | avg(ir.rating) | ir.item_id |
+---------+-----------+----------------+------------+
| 1 | Item 1 | 7.5 | 1 |
+---------+-----------+----------------+------------+
对于 NULL 我们有:
+---------+-----------+-----------+------------+
| item_id | item_name | ir.rating | ir.item_id |
+---------+-----------+-----------+------------+
| 2 | Item 2 | NULL | NULL |
| 4 | Item 4 | NULL | NULL |
| 5 | Item 5 | NULL | NULL |
+---------+-----------+-----------+------------+
折叠成:
+---------+-----------+----------------+------------+
| item_id | item_name | avg(ir.rating) | ir.item_id |
+---------+-----------+----------------+------------+
| 2| Item 2 | NULL | NULL |
+---------+-----------+----------------+------------+
对于 3 我们有:
+---------+-----------+-----------+------------+
| item_id | item_name | ir.rating | ir.item_id |
+---------+-----------+-----------+------------+
| 3 | Item 3 | 10 | 3 |
+---------+-----------+-----------+------------+
折叠成:
+---------+-----------+----------------+------------+
| item_id | item_name | avg(ir.rating) | ir.item_id |
+---------+-----------+----------------+------------+
| 3 | Item 3 | 10 | 3 |
+---------+-----------+----------------+------------+
合并三个折叠的结果给出:
+---------+-----------+----------------+------------+
| item_id | item_name | avg(ir.rating) | ir.item_id |
+---------+-----------+----------------+------------+
| 1 | Item 1 | 7.5 | 1 |
| 3 | Item 3 | 10 | 3 |
| 2 | Item 2 | NULL | NULL |
+---------+-----------+----------------+------------+
这就是你得到的。
一个棘手的部分是 NULL 行折叠的方式。回想一下,这些是空行:
+---------+-----------+-----------+------------+
| item_id | item_name | ir.rating | ir.item_id |
+---------+-----------+-----------+------------+
| 2 | Item 2 | NULL | NULL |
| 4 | Item 4 | NULL | NULL |
| 5 | Item 5 | NULL | NULL |
+---------+-----------+-----------+------------+
当您进行分组时,大多数数据库系统甚至不允许您选择不属于分组的列。 MySQL 是个异常(exception)。由于您仅在 ir.rating 上分组,因此这是唯一一个最能让您选择的选项,因为没有明确的方法以非聚合方式折叠三行。 MySQL 所做的只是选择它遇到的第一个并使用该行中的值作为折叠值。所以 (2,4,5) => (2) and (Item 2, Item 4, Item 5) => Item 2 and (NULL, NULL, NULL) => NULL。这就是您只看到第 2 行的原因(您实际上看到了三个看起来像第 2 行的折叠行)。
要真正了解这一点并将重点带回家,请考虑以下查询:
select group_concat(i.item_id), group_concat(i.item_name), avg(ir.rating) from Items i left join ItemRating ir ON ir.item_id = i.item_id group by ir.item_id;
这就像您的原始查询一样,只是所有三个选定的列现在都具有组聚合函数。我正在使用 GROUP_CONCAT,它只是连接字符串以形成折叠版本(这在除 MySQL 之外的其他 SQL 系统中有效)。返回这个:
+-------------------------+---------------------------+----------------+
| group_concat(i.item_id) | group_concat(i.item_name) | avg(ir.rating) |
+-------------------------+---------------------------+----------------+
| 2,4,5 | Item 2,Item 4,Item 5 | NULL |
| 1,1 | Item 1,Item 1 | 7.5000 |
| 3 | Item 3 | 10.0000 |
+-------------------------+---------------------------+----------------+
关于mysql - 这个带有左连接和分组依据(在错误的列上)的 MYSQL 查询中发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8348658/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象