0

我想用 2 个语句划分多个列,如下所示:

TBL1
NAME  VAL1 VAL2 VAL3
A     2    3      3

TBL2
NAME  VAL1 VAL2 VAL3
B     2    3      3

错误脚本

select (select * from tbl1)/(select * from TBL2) as result

Result that i need as the following:

VAL1 VAL2 VAL3
2/2  3/3  3/3 
4

3 回答 3

0

应该有一个ON子句,但不确定它应该是什么

SELECT t1.VAL1/t2.VAL1, t1.VAL2/t2.VAL2, t1.VAL3/t2.VAL3, 
FROM TBL1 t1, TBL2 t2
于 2013-06-04T04:30:49.197 回答
0

我能想到的最好的事情是

SET @COUNTER1 = 0;
SET @COUNTER2 = 0;
SELECT T1.VAL1 / T2.VAL1,
       T1.VAL2 / T2.VAL2,
       T1.VAL3 / T2.VAL3
FROM       (SELECT *, (@COUNTER1 := @COUNTER1 + 1) AS id FROM TBL1) AS T1
INNER JOIN (SELECT *, (@COUNTER2 := @COUNTER2 + 1) AS id FROM TBL2) AS T2
ON         T1.id = T2.id
于 2013-06-04T04:59:14.730 回答
-1
Select Tbl1.Val1 / Tbl2.Val1 As Val1
    , Tbl1.Val2 / Tbl2.Val2 As Val2
    , Tbl1.Val3 / Tbl2.Val3 As Val3
From Tbl1
    Cross Join Tbl2

当然,这可能不是你想要的。首先,表 1 中的行与表 2 中的行没有任何关联。即,如果两个表各有 3 行,则结果将有 9 行。简而言之,您将在两个表之间获得笛卡尔积。其次,没有逻辑来处理除以零错误。这些值是否应该简单地设置为零?他们应该为空吗?

MySQL 连接语法。(是的,MySQL 支持 ISO/ANSI 标准交叉连接语法)。

SQL 小提琴版本

编辑

如果要尝试将值连接到 的字符串表达式中#/#,则需要使用该Concat函数:

Select Concat(Tbl1.Val1,'/',Tbl2.Val1) As Val1
  , Concat(Tbl1.Val2,'/',Tbl2.Val2) As Val2
  , Concat(Tbl1.Val3,'/',Tbl2.Val3) As Val3
From Tbl1
    Cross Join Tbl2

SQL 小提琴版本。

于 2013-06-04T04:31:06.553 回答