在sql中乘以两个varchars

时间:2015-09-18 14:34:12

标签: sql mariadb varchar

这是我尝试过的,但它给出了错误:

MariaDB [test]> create table table1 (length varchar, breadth varchar);                                                             
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' breadth varchar)' at line 1
MariaDB [test]> create table table1 (length varchar(20), breadth varchar(20));                                                         
Query OK, 0 rows affected (0.06 sec)
MariaDB [test]> insert into table1 (length, breadth) values ('12','11');                                                                                                                               
Query OK, 1 row affected (0.05 sec)                                                                                                                                                                    

MariaDB [test]> select * from table1;                                                                                                                                                                  
+--------+---------+                                                                                                                                                                                   
| length | breadth |                                                                                                                                                                                   
+--------+---------+                                                                                                                                                                                   
| 12     | 11      |                                                                                                                                                                                   
+--------+---------+                                                                                                                                                                                   
1 row in set (0.02 sec)                                                                                                                                                                                                                                                                                                                                                                                   
MariaDB [test]> select convert (int, length)*convert(int, breadth) as T from table1;                                                                                                                   
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'int, length)*convert(int, breadth) as T fr
om table1' at line 1                                                                                                                                                                                   
MariaDB [test]> select convert(int, length)*convert(int, breadth) as T from table1;                                                                                                                    
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'int, length)*convert(int, breadth) as T fr
om table1' at line 1                                                                                                                                                                                   

1 个答案:

答案 0 :(得分:1)

使用cast()

select cast(length as int) * cast(breadth as int) as T
from table1;  

documentation中所述,convert()用于在不同字符集之间进行转换。您正在混淆MySQL(以及MariaDB' s)对{1}}与SQL Server的使用。

顺便说一下,你甚至不需要显式convert()(至少在MySQL中)。引擎将为您进行隐式转换:

cast()

虽然这是支持的,但我并不是真的提倡取决于隐式演员。

Here是一个SQL小提琴。