建表语句:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for testcaculate
-- ----------------------------
DROP TABLE IF EXISTS `testcaculate`;
CREATE TABLE `testcaculate` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`city` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
- CONCAT(str1,str2,…)函数练习 将查询结果拼接到一起,组成单个值
INSERT INTO `testsql`.`testcaculate`(`id`, `country`, `city`) VALUES (1, '中国', '北京');
INSERT INTO `testsql`.`testcaculate`(`id`, `country`, `city`) VALUES (2, '美国', '旧金山');
SELECT
CONCAT( country, "_", city ) councity
FROM
testcaculate;
- TRIM(str)函数去掉左右两边空格,LTRIM(str)去掉左边空格,RTRIM(str)去掉右边空格
INSERT INTO `testsql`.`testcaculate`(`id`, `country`, `city`) VALUES (3, ' 日本', '东京');
INSERT INTO `testsql`.`testcaculate`(`id`, `country`, `city`) VALUES (4, '法国', '巴黎 ');
INSERT INTO `testsql`.`testcaculate`(`id`, `country`, `city`) VALUES (5 , ' 法国 ', ' 巴黎 ');
- 查询有空格
SELECT
CONCAT( country, "_", city ) councity
FROM
testcaculate;
-- 取掉左边空格
SELECT
CONCAT( LTRIM(country), "_", city ) councity
FROM
testcaculate;
-- 取掉右边空格
SELECT
CONCAT( RTRIM(country), "_", city ) councity
FROM
testcaculate;
-- 取掉左右两边空格
SELECT
CONCAT( TRIM(country), "_", TRIM(city) ) councity
FROM
testcaculate;
- select 中可以直接进行字段的±*/运算。