在 sql 中乘以两个 varchars
multiply two varchars in sql
这是我尝试过的方法,但出现错误:
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
使用cast()
:
select cast(length as int) * cast(breadth as int) as T
from table1;
如documentation中所述,convert()
用于不同字符集之间的转换。您将 MySQL(和 MariaDB)对 convert()
的使用与 SQL 服务器的使用混淆了。
顺便说一下,您甚至不需要明确的 cast()
(至少在 MySQL 中)。引擎将为您进行隐式转换:
select length * breadth as T from table1;
虽然支持,但我并不提倡依赖隐式转换。
Here 是 SQL Fiddle.
这是我尝试过的方法,但出现错误:
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
使用cast()
:
select cast(length as int) * cast(breadth as int) as T
from table1;
如documentation中所述,convert()
用于不同字符集之间的转换。您将 MySQL(和 MariaDB)对 convert()
的使用与 SQL 服务器的使用混淆了。
顺便说一下,您甚至不需要明确的 cast()
(至少在 MySQL 中)。引擎将为您进行隐式转换:
select length * breadth as T from table1;
虽然支持,但我并不提倡依赖隐式转换。
Here 是 SQL Fiddle.