Heim > Backend-Entwicklung > PHP-Tutorial > mysql查询将两列数值相加问题

mysql查询将两列数值相加问题

WBOY
Freigeben: 2016-06-06 20:33:24
Original
4802 Leute haben es durchsucht

如图所示:
我希望加一个total_price列,值为price+price2的和,这个应该怎么写SQL;

select * from ims_goods_1 where price!=0 and price2!=0;

mysql查询将两列数值相加问题

奉上SQL文件:

<code>/*
Navicat MySQL Data Transfer

Source Server         : car
Source Server Version : 50518
Source Host           : wsy19950503.mysql.rds.aliyuncs.com:3306
Source Database       : car

Target Server Type    : MYSQL
Target Server Version : 50518
File Encoding         : 65001

Date: 2015-06-11 15:40:28
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `ims_goods_1`
-- ----------------------------
DROP TABLE IF EXISTS `ims_goods_1`;
CREATE TABLE `ims_goods_1` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `type` tinyint(1) NOT NULL COMMENT '1为小保养,2为电瓶',
  `name` varchar(255) NOT NULL DEFAULT '' COMMENT '商品名称',
  `price` int(4) NOT NULL COMMENT '产品价格',
  `price2` int(4) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of ims_goods_1
-- ----------------------------
INSERT INTO `ims_goods_1` VALUES ('1', '1', '曼牌Mann', '70', '0');
INSERT INTO `ims_goods_1` VALUES ('2', '1', '马勒', '29', '0');
INSERT INTO `ims_goods_1` VALUES ('13', '1', '汉格斯特Hengsr', '68', '0');
INSERT INTO `ims_goods_1` VALUES ('4', '2', '嘉实多 金嘉护SN 10W-40', '152', '0');
INSERT INTO `ims_goods_1` VALUES ('5', '2', '嘉实多 磁护SN 5W-40', '248', '110');
INSERT INTO `ims_goods_1` VALUES ('6', '2', '嘉实多 极护SN 0W-40', '376', '125');
INSERT INTO `ims_goods_1` VALUES ('7', '2', '壳牌 黄喜力HX5 10W-40', '138', '0');
INSERT INTO `ims_goods_1` VALUES ('8', '2', '壳牌  灰喜力ULTRA 5W-40', '380', '0');
INSERT INTO `ims_goods_1` VALUES ('10', '2', '美孚 美孚1号 0W-40', '368', '113');

</code>
Nach dem Login kopieren
Nach dem Login kopieren

回复内容:

如图所示:
我希望加一个total_price列,值为price+price2的和,这个应该怎么写SQL;

select * from ims_goods_1 where price!=0 and price2!=0;

mysql查询将两列数值相加问题

奉上SQL文件:

<code>/*
Navicat MySQL Data Transfer

Source Server         : car
Source Server Version : 50518
Source Host           : wsy19950503.mysql.rds.aliyuncs.com:3306
Source Database       : car

Target Server Type    : MYSQL
Target Server Version : 50518
File Encoding         : 65001

Date: 2015-06-11 15:40:28
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `ims_goods_1`
-- ----------------------------
DROP TABLE IF EXISTS `ims_goods_1`;
CREATE TABLE `ims_goods_1` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `type` tinyint(1) NOT NULL COMMENT '1为小保养,2为电瓶',
  `name` varchar(255) NOT NULL DEFAULT '' COMMENT '商品名称',
  `price` int(4) NOT NULL COMMENT '产品价格',
  `price2` int(4) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of ims_goods_1
-- ----------------------------
INSERT INTO `ims_goods_1` VALUES ('1', '1', '曼牌Mann', '70', '0');
INSERT INTO `ims_goods_1` VALUES ('2', '1', '马勒', '29', '0');
INSERT INTO `ims_goods_1` VALUES ('13', '1', '汉格斯特Hengsr', '68', '0');
INSERT INTO `ims_goods_1` VALUES ('4', '2', '嘉实多 金嘉护SN 10W-40', '152', '0');
INSERT INTO `ims_goods_1` VALUES ('5', '2', '嘉实多 磁护SN 5W-40', '248', '110');
INSERT INTO `ims_goods_1` VALUES ('6', '2', '嘉实多 极护SN 0W-40', '376', '125');
INSERT INTO `ims_goods_1` VALUES ('7', '2', '壳牌 黄喜力HX5 10W-40', '138', '0');
INSERT INTO `ims_goods_1` VALUES ('8', '2', '壳牌  灰喜力ULTRA 5W-40', '380', '0');
INSERT INTO `ims_goods_1` VALUES ('10', '2', '美孚 美孚1号 0W-40', '368', '113');

</code>
Nach dem Login kopieren
Nach dem Login kopieren

select t.*,price+price2 as total_price from ims_goods_1 t where price!=0 and price2!=0;

如果这里的+换成一个比较复杂的运算,比如求这两个数的阶乘的和作为新的列,这个该怎么弄

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage