【发布时间】:2015-12-28 19:45:57
【问题描述】:
我有一张带有下一个结构的表
表 A:
| Id | 1 | 2 | 3 |
|-----|---------|----------|-----------|
| 1 | 00:05:00| 00:10:00 | (null) |
| 2 | 00:10:00| (null) | (null) |
表 B
| Id | col |Expected |
|-----|---------|----------|
| 1 | 1 | 00:06:00 |
| 2 | 2 | 00:12:00 |
| 3 | 3 | 00:22:00 |
我试图根据表 A 的行上的实际值与表 B 上的预期值进行比较
Select
Id, (Select ??????? From (Select TiempoStd from B)as Stime) as Time
From
A
基本上我想在两张表之间进行比较,看看哪一张更大,然后将其添加到下一张。
我无法理解如何在我的临时表 Stime 下调用特定值。
我对 SQL 不是很熟悉,所以这就是我无法理解的原因,逻辑是这样的。问号在查询中的位置
ADDTIME(IF(A.1>(Stime.Expected where col = 1),A.1,(Stime.Expected where col = 1)),
ADDTIME(IF(A.2>(Stime.Expected where col = 2),A.2,(Stime.Expected where col = 2)),
IF(A.3>(Stime.Expected where col = 3),A.3,(Stime.Expected where col = 3))
Stime.Expected where col = 3 是一个错误的语法,对吧?但我希望你能理解我试图在这里提出的逻辑。
所以输出会是这样的
| Id | Time |
|-----|---------|
| 1 | 00:40:00|
| 2 | 00:44:00|
【问题讨论】:
-
您确实应该在两个表中使用相同的结构。如果你改变表 1 使列是不同的行,你可以做一个普通的 JOIN。
-
@Barmar 怎么会这样?我不认为我明白你想说的,可能是一个解决方案
-
@Barmar 我在表 B 中的行数将始终与表 A 中的列数(除 Id 外)相同
-
如果我可以像访问数组一样访问结果,我的逻辑将是这样的。
(SELECT ADDTIME(IF(TiempoStd[0]>A.1,TiempoStd[0],A.1),ADDTIME(IF(TiempoStd[1]>A.2,TiempoStd[1],A.2),IF(TiempoStd[2]>A.3,TiempoStd[2],A.3)) From (Select TiempoStd from Procesos)as ti) as Cierre
标签: mysql