【问题标题】:Left join with calculated column and different schemas?左连接计算列和不同的模式?
【发布时间】:2021-08-30 03:12:35
【问题描述】:

我想对下列表格执行左联接:

Vehicles.boats,
vehicle_details.colors

有栏目

 vehicles.boats.yacht
 color_id

其中color_id 是根据vehicle_details.colors 在涉及的计算中计算得出的

vehicle_details.colors.sequence
 and 
vehicle_details.colors.name

我假设以下将作为我的骨架,但我不确定将定义 color_id 的计算放在哪里:

SELECT vehicles.boats.yacht, vehicle_details.colors.sequence
FROM vehicles.boats
LEFT JOIN vehicle_details.colors 
ON vehicle.boats.colorIdentifier = color_id;

会不会像下面这样,在ON 部分使用计算?

SELECT vehicles.boats.yacht, vehicle_details.colors.sequence
FROM vehicles.boats
LEFT JOIN vehicle_details.colors 
ON vehicle.boats.colorIdentifier = *calculations* AS color_id;

【问题讨论】:

  • 我迷路了。哪些表有哪些列?你在做什么神秘的计算?你的实际问题是什么?如果您问是否可以在on 子句中进行计算,答案是“可以”。

标签: mysql sql left-join hql calculated-columns


【解决方案1】:

在这种格式下,您需要进行两次计算,一次用于连接条件,一次用于显示。

SELECT vehicles.boats.yacht, vehicle_details.colors.sequence,*calculations* AS color_id;
FROM vehicles.boats
LEFT JOIN vehicle_details.colors 
ON vehicles.boats.colorIdentifier = *calculations*;

您可以使用子查询并执行一次,例如:

SELECT vehicles.boats.yacht, vehicle_details.colors.sequence, color_id;
FROM vehicles.boats
LEFT JOIN ( Select vehicle_details.colors, *calculations* as colour_id from vehicle_details) as Details_subquery
ON vehicles.boats.colorIdentifier = colour_id ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-12
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2011-01-12
    相关资源
    最近更新 更多