mysql - 在sql中使用group by连接两个表
问题描述
您好,我正在尝试加入这两个表,但无法加入。我单独拥有数据,但不幸的是我无法加入它们,因为表 nr 2 中涉及 group by 语句,这使得加入有点复杂。
表 1;-- 包括所有数据
SELECT i.attr,iv.dateofvalue,iv.price
FROM information i, informationvalues iv
WHERE i.attr=iv.attr;
表 nr2;-- 仅包括不包括价格的最新数据
SELECT i.attr, MAX (iv.dateofvalue) AS recentdate
FROM information i, informationvalues iv
WHERE i.attr=iv.attr
GROUP BY i.attr;
目标是通过从 table nr 1 获取 tablenr 2 以相应的价格扩展它。注意:recentdate=dateofvalue 希望有人能提供帮助,在此先感谢!
解决方案
您似乎正在寻找每个属性的最新价格。如果是这样,您可以使用相关子查询进行过滤,该子查询为您提供每个属性的最后日期:
select
i.attr,
v.dateofvalue,
v.price
from information i
inner join informationvalues v on v.attr = i.attr
where v.dateofvalue = (
select max(v1.dateofvalue) from informationvalues v1 where v1.attr = v.attr
)
推荐阅读
- java - Java如何禁用特定的编译优化算法(例如GVN)
- ios - 自定义键盘 Swift 中的字符预览无法正确显示
- python - 替换超过 1 个具有相同含义的单词而不用 1 加 1
- python-3.x - 在 Python 中,如何将 astype(str) 应用于整个数据帧
- javascript - JQuery Angular 表单验证
- javascript - 带有详细信息+摘要的剧透标签
- javascript - 使用jQuery将多个输入类型日期放入href
- python - 在这种情况下,我不知道如何打印 5。我也尝试过 return
- flopy - 当一个包调用write_file()函数时如何控制数组格式,比如DISU包?
- python - 从 python 运行一个新的进程/可执行文件,它保持运行