mysql数据库实操笔记20170418

一、建立商品分类表和价格表;

1.分类表
`sankeq``sankeq`
CREATE TABLE cs_mysql11(
id INT(11) NOT NULL AUTO_INCREMENT,
category VARCHAR(20) NOT NULL,
parentid INT(11) NOT NULL DEFAULT 0,
ctime INT(11) NOT NULL,
PRIMARY KEY (id)
)ENGINE=INNODB DEFAULT CHARSET=utf8
 COLLATE=utf8_unicode_ci;
`sankeq`
2.商品表
CREATE TABLE cs_hwsp(
id INT(11) NOT NULL AUTO_INCREMENT COMMENT '商品ID',
cateid INT(11) NOT NULL COMMENT '商品ID分类',
goodname CHAR(30) NOT NULL COMMENT '商品名称',
price FLOAT(7,2) NOT NULL COMMENT '商品单价',
markamout FLOAT(7,2) NOT NULL COMMENT '商品市场价',
quantity SMALLINT(5) NOT NULL DEFAULT 0 COMMENT '商品数量',
createtime INT(11) NOT NULL COMMENT '记录时间',
PRIMARY KEY (id),
INDEX (goodname)
)ENGINE=INNODB DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci;
TRUNCATE TABLE `cs_mysql11`
INSERT INTO `sankeq`.`cs_mysql11` (
  `category`,
  `parentid`,
  `ctime`
)
VALUES
  (
    'category',
    'parentid',
    'ctime'
  ) ;

二、利用PHP向数据库中的商品表插入数据;

1.向分类表插入数据

<?php
echo  "<meta charset = 'utf-8'>";
include '/lianjiesql.php';
$time=$_SERVER['REQUEST_TIME'];
for($i=1;$i<=500;$i++){
$sql = "INSERT  INTO  `cs_mysql11`" ;
 $sql .="(`category`,`parentid`,`ctime`) ";
$sql .="  VALUES  ( '分类$i','0', '$time');" ;

$bb=mysql_query($sql);
if($bb){
}else{
    echo mysql_errno();
}
}
?>

2.向商品单价表插入数据;

<?php
echo "<meta charset='utf-8'>";
include '/lianjiesql.php';
$sqlsz
=
array("韩版男装休闲","韩版男装潮流","韩版女装休闲","韩版女装潮流","日版男装休闲","日版男装潮流","日版女装休闲","日版女装潮流","国版男装休闲","国版男装潮流","国版女装休闲","国版女装潮流","美版男装休闲","美版男装潮流","美版女装休闲","美版女装潮流","英版男装休闲","英版男装潮流","英版女装休闲","英版女装潮流");
//var_dump($sqlsz);
for($i=0;$i<20;$i++){
    $k=$i+1234567890;
    $j=$i+1;
    $q=rand(5000,10000)/100;
$sql ="INSERT INTO `cs_hwsp` (";
$sql .="`id`,`cateid`,`goodname`,`price`,`markamout`,`quantity`,`createtime`)";
$sql .=" VALUES ('$k','$j','$sqlsz[$i]','".$q."','".$q*1.2."','".rand(10,100)."','".rand(1485878400,1492498567)."');";
$aa=mysql_query($sql);
if($aa){
    }else{
        echo  mysql_errno();
        echo  "<br>".mysql_error();
    }
}
?>

三、利用sql函数查看数据表各种信息的方法;

转换时间格式
SELECT FROM_UNIXTIME(createtime) FROM cs_hwsp

获取当前时间
SELECT UNIX_TIMESTAMP();

DISTINCT()过滤重复
SELECT DISTINCT(cateid) FROM cs_hwsp

COUNT()统计个数
SELECT COUNT(DISTINCT(cateid)) FROM cs_hwsp ORDER BY cateid
SELECT COUNT(*) FROM cs_hwsp

SUM()求和
求price列求和
SELECT SUM(price) FROM cs_hwsp
求每个月总销售额
SELECT SUM(price),SUBSTRING(FROM_UNIXTIME(createtime),1,7) AS ymonth FROM cs_hwsp GROUP BY ymonth;
求每天总销售额
SELECT SUM(price),DATE(FROM_UNIXTIME(createtime)) AS ymonth FROM cs_hwsp GROUP BY ymonth ORDER BY ymonth DESC;
求每天销售额大于100的记录
SELECT
SUM(price) AS total,DATE(FROM_UNIXTIME(createtime)) AS ymonth FROM
cs_hwsp GROUP BY ymonth HAVING total>100 ORDER BY ymonth DESC;

AVG()求平均
求所有商品平均单价
SELECT AVG(price) FROM cs_hwsp;
求每个分类商品平均单价
SELECT AVG(a.price),a.cateid,b.category FROM cs_hwsp a INNER JOIN cs_mysql11 b ON(a.cateid=b.id) GROUP BY cateid;

MAX()求最大值
SELECT MAX(a.price),a.cateid,b.category FROM cs_hwsp a INNER JOIN cs_mysql11 b ON(a.cateid=b.id) GROUP BY cateid;

MIN()求最小值
SELECT MIN(a.price),a.cateid,b.category FROM cs_hwsp a INNER JOIN cs_mysql11 b ON(a.cateid=b.id) GROUP BY cateid;

上一篇:线上CPU100%排查


下一篇:AdminLTE 前端框架