목차
回复内容:
백엔드 개발 PHP 튜토리얼 mysql查询将两列数值相加问题

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

Jun 06, 2016 pm 08:33 PM
mysql php sql

如图所示:
我希望加一个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>
로그인 후 복사
로그인 후 복사

回复内容:

如图所示:
我希望加一个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>
로그인 후 복사
로그인 후 복사

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

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

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

CakePHP 날짜 및 시간

CakePHP 프로젝트 구성 CakePHP 프로젝트 구성 Sep 10, 2024 pm 05:25 PM

CakePHP 프로젝트 구성

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

CakePHP 파일 업로드

CakePHP 라우팅 CakePHP 라우팅 Sep 10, 2024 pm 05:25 PM

CakePHP 라우팅

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP 토론

MySQL 8.4에서 mysql_native_password가 로드되지 않음 오류를 수정하는 방법 MySQL 8.4에서 mysql_native_password가 로드되지 않음 오류를 수정하는 방법 Dec 09, 2024 am 11:42 AM

MySQL 8.4에서 mysql_native_password가 로드되지 않음 오류를 수정하는 방법

CakePHP 빠른 가이드 CakePHP 빠른 가이드 Sep 10, 2024 pm 05:27 PM

CakePHP 빠른 가이드

See all articles