sqlite - SQLite 查询中选择语句的别名
问题描述
有什么方法可以使用别名来避免select
在此查询中重复嵌套语句?
select distinct
ingot_sales.date_time as date,
ingot_sales.name as buyer,
ingot_sales.metal as metal,
(select price from ingot_list where ingot_sales.metal = ingot_list.metal) as unit_price,
ingot_sales.quantity as quantity,
(select price from ingot_list where ingot_sales.metal = ingot_list.metal) * quantity as total_price,
ingot_sales.paid as paid
from ingot_sales, ingot_list;
我尝试使用unit_price
代替第二个 nested select
,但不断得到重复的行,每个行具有不同的计算值。
解决方案
您可以加入表,这样您就不需要相关的子查询:
select distinct
s.date_time as date,
s.name as buyer,
s.metal as metal,
l.price as unit_price,
s.quantity as quantity,
l.price * s.quantity as total_price,
s.paid as paid
from ingot_sales s left join ingot_list l
on s.metal = l.metal
推荐阅读
- java - 为什么我会到这里 java.io.EOFException?
- java - 日期时间值不正确:函数 str_to_date 的“(?)??????????(NEW)”
- c# - 如何从 .csproj 文件中确定 MVC 项目
- html - 我的图片库中某些图片的纵横比错误
- android - DaggerIntentService pug:没有注入器工厂绑定到 Class<>
- flutter - 为 setState() 制作通用函数
- java - 如何使用 Java Stream API 解决滑动窗口问题?
- c# - 无法使用 ADO.net 在 C# 中插入和查看数据
- c# - 打印保存在数组c#中的名称和密码
- php - 为什么服务器响应加载资源失败:服务器响应状态为 500(内部服务器错误)?我该如何解决?