mysql语句集(一)

SELECT
            UcenterTdouLog.user_id, UcenterTdouLog.subject, UcenterTdouLog.tdou,UcenterTdouLog.created,
            UcenterTdouLog.out_id, ifnull(SaleOrder.order_sn, 0) AS order_sn,
            (
                SELECT

                    SUM(tdou)
                FROM
                    ucenter_tdou_log AS a
                WHERE
                    a.created <= UcenterTdouLog.created
                    AND a.user_id = UcenterTdouLog.user_id
                GROUP BY
                    a.user_id
            ) AS tdou_total
        FROM
            ucenter_tdou_log AS UcenterTdouLog
            LEFT JOIN sale_order AS SaleOrder ON SaleOrder.order_id = UcenterTdouLog.out_id
        WHERE
             1=1 %s
        ORDER BY
             UcenterTdouLog.user_id asc,UcenterTdouLog.created asc

  收发存汇总

SELECT
             GoodsSku.sku_id,
             GoodsSku.barcode,
             GoodsSku.sku_name,
             GoodsTypeSpec1.spec_name AS sku_spec1,
             GoodsTypeSpec2.spec_name AS sku_spec2,
             GoodsCategory1.cat_name AS catName1,
             GoodsCategory2.cat_name AS catName2,
             GoodsCategory3.cat_name AS catName3,
             GoodsUnit.sale_unit,
             SellerBase.seller_name,
             IFNULL((SELECT SUM(WmsBatchItem.sku_num) FROM wms_batch_base AS WmsBatchBase INNER JOIN wms_batch_item AS WmsBatchItem ON WmsBatchItem.batch_id = WmsBatchBase.batch_id WHERE WmsBatchItem.sku_id = GoodsSku.sku_id %s),0) AS qichuNum,
             IFNULL((SELECT SUM(WmsBatchItem.sku_num * WmsBatchItem.sku_price ) FROM wms_batch_base AS WmsBatchBase INNER JOIN wms_batch_item AS WmsBatchItem ON WmsBatchItem.batch_id = WmsBatchBase.batch_id WHERE WmsBatchItem.sku_id = GoodsSku.sku_id %s),0) AS qichuPrice,
             IFNULL((SELECT SUM(WmsBatchItem.sku_num) FROM wms_batch_base AS WmsBatchBase INNER JOIN wms_batch_item AS WmsBatchItem ON WmsBatchItem.batch_id = WmsBatchBase.batch_id WHERE WmsBatchItem.sku_id = GoodsSku.sku_id AND WmsBatchBase.type_id not in(02,05) %s),0) AS batchNum,
             IFNULL((SELECT SUM(WmsBatchItem.sku_num * WmsBatchItem.sku_price ) FROM wms_batch_base AS WmsBatchBase INNER JOIN wms_batch_item AS WmsBatchItem ON WmsBatchItem.batch_id = WmsBatchBase.batch_id WHERE WmsBatchItem.sku_id = GoodsSku.sku_id AND WmsBatchBase.type_id not in(02,05) %s),0) AS batchPrice,
             IFNULL((SELECT SUM(WmsBatchItem.sku_num) FROM wms_batch_base AS WmsBatchBase INNER JOIN wms_batch_item AS WmsBatchItem ON WmsBatchItem.batch_id = WmsBatchBase.batch_id WHERE WmsBatchItem.sku_id = GoodsSku.sku_id AND WmsBatchBase.type_id = 02 %s),0) AS allocBatchNum,
             IFNULL((SELECT SUM(WmsBatchItem.sku_num * WmsBatchItem.sku_price) FROM wms_batch_base AS WmsBatchBase INNER JOIN wms_batch_item AS WmsBatchItem ON WmsBatchItem.batch_id = WmsBatchBase.batch_id WHERE WmsBatchItem.sku_id = GoodsSku.sku_id AND WmsBatchBase.type_id = 02 %s),0) AS allocBatchPrice,
             IFNULL((SELECT SUM(WmsBatchItem.sku_num) FROM wms_batch_base AS WmsBatchBase INNER JOIN wms_batch_item AS WmsBatchItem ON WmsBatchItem.batch_id = WmsBatchBase.batch_id WHERE WmsBatchItem.sku_id = GoodsSku.sku_id AND WmsBatchBase.type_id = 05 %s),0) AS takeBatchNum,
             IFNULL((SELECT SUM(WmsBatchItem.sku_num * WmsBatchItem.sku_price) FROM wms_batch_base AS WmsBatchBase INNER JOIN wms_batch_item AS WmsBatchItem ON WmsBatchItem.batch_id = WmsBatchBase.batch_id WHERE WmsBatchItem.sku_id = GoodsSku.sku_id AND WmsBatchBase.type_id = 05 %s),0) AS takeBatchPrice,
             IFNULL((SELECT SUM(WmsOrderItem.sku_num) FROM wms_order_base AS WmsOrderBase INNER JOIN wms_order_item AS WmsOrderItem ON WmsOrderItem.order_id = WmsOrderBase.order_id WHERE WmsOrderItem.sku_id = GoodsSku.sku_id AND WmsOrderBase.type_id not in (01,05) %s),0) AS OrderNum,
             IFNULL((SELECT SUM(WmsOrderItem.sku_num * WmsOrderItem.stock_price) FROM wms_order_base AS WmsOrderBase INNER JOIN wms_order_item AS WmsOrderItem ON WmsOrderItem.order_id = WmsOrderBase.order_id WHERE WmsOrderItem.sku_id = GoodsSku.sku_id AND WmsOrderBase.type_id not in (01,05) %s),0) AS OrderPrice,
             IFNULL((SELECT SUM(WmsOrderItem.sku_num) FROM wms_order_base AS WmsOrderBase INNER JOIN wms_order_item AS WmsOrderItem ON WmsOrderItem.order_id = WmsOrderBase.order_id WHERE WmsOrderItem.sku_id = GoodsSku.sku_id AND WmsOrderBase.type_id = 01 %s),0) AS allocOrderNum,
             IFNULL((SELECT SUM(WmsOrderItem.sku_num * WmsOrderItem.stock_price) FROM wms_order_base AS WmsOrderBase INNER JOIN wms_order_item AS WmsOrderItem ON WmsOrderItem.order_id = WmsOrderBase.order_id WHERE WmsOrderItem.sku_id = GoodsSku.sku_id AND WmsOrderBase.type_id = 01 %s), 0) AS allocOrderPrice,
             IFNULL((SELECT SUM(WmsOrderItem.sku_num) FROM wms_order_base AS WmsOrderBase INNER JOIN wms_order_item AS WmsOrderItem ON WmsOrderItem.order_id = WmsOrderBase.order_id WHERE WmsOrderItem.sku_id = GoodsSku.sku_id AND WmsOrderBase.type_id = 05 %s),0) AS takeOrderNum,
             IFNULL((SELECT SUM(WmsOrderItem.sku_num * WmsOrderItem.stock_price) FROM wms_order_base AS WmsOrderBase INNER JOIN wms_order_item AS WmsOrderItem ON WmsOrderItem.order_id = WmsOrderBase.order_id WHERE WmsOrderItem.sku_id = GoodsSku.sku_id AND WmsOrderBase.type_id = 05 %s), 0) AS takeOrderPrice
        FROM
             goods_sku AS GoodsSku
             INNER JOIN goods_base AS GoodsBase ON GoodsSku.goods_id = GoodsBase.goods_id
             LEFT JOIN goods_category AS GoodsCategory1 ON GoodsCategory1.cat_id = GoodsBase.cat_id1
             LEFT JOIN goods_category AS GoodsCategory2 ON GoodsCategory2.cat_id = GoodsBase.cat_id2
             LEFT JOIN goods_category AS GoodsCategory3 ON GoodsCategory3.cat_id = GoodsBase.cat_id3
             LEFT JOIN goods_unit AS GoodsUnit ON GoodsUnit.unit_id = GoodsBase.unit_id
             LEFT JOIN seller_base AS SellerBase ON SellerBase.seller_id = GoodsBase.seller_id
             LEFT JOIN goods_type_spec AS GoodsTypeSpec1 ON GoodsTypeSpec1.spec_id = GoodsSku.sku_spec1
             LEFT JOIN goods_type_spec AS GoodsTypeSpec2 ON GoodsTypeSpec2.spec_id = GoodsSku.sku_spec2
        LIMIT %d,%d

  

/**
     * 收发存汇总报表
     * @param $houseID
     * @param $startDate
     * @param $endDate
     * @param $page
     * @param int $size
     * @return array
     */
    public function fetchOrderBatchAllReport($houseID, $startDate, $endDate, $page, $size = 20)
    {
        $database = $this->database();
        $where1 = ‘‘;
        $where2 = ‘‘;
        $where3 = ‘‘;
        $where4 = ‘‘;
        $where5 = ‘‘;
        $where6 = ‘‘;
        $where7 = ‘‘;
        $where8 = ‘‘;
        $where9 = ‘‘;
        $where10 = ‘‘;
        $where11 = ‘‘;
        $where12 = ‘‘;
        $where13 = ‘‘;
        $where14 = ‘‘;

        $params1 = array();
        $params2 = array();
        $params3 = array();
        $params4 = array();
        $params5 = array();
        $params6 = array();
        $params7 = array();
        $params8 = array();
        $params9 = array();
        $params10 = array();
        $params11 = array();
        $params12 = array();
        $params13 = array();
        $params14 = array();
        if ($houseID) {
            $where1 .= ‘ AND WmsBatchBase.house_id = ?‘;
            $where2 .= ‘ AND WmsBatchBase.house_id = ?‘;
            $where3 .= ‘ AND WmsBatchBase.house_id = ?‘;
            $where4 .= ‘ AND WmsBatchBase.house_id = ?‘;
            $where5 .= ‘ AND WmsBatchBase.house_id = ?‘;
            $where6 .= ‘ AND WmsBatchBase.house_id = ?‘;
            $where7 .= ‘ AND WmsBatchBase.house_id = ?‘;
            $where8 .= ‘ AND WmsBatchBase.house_id = ?‘;
            $where9 .= ‘ AND WmsOrderBase.house_id = ?‘;
            $where10 .= ‘ AND WmsOrderBase.house_id = ?‘;
            $where11 .= ‘ AND WmsOrderBase.house_id = ?‘;
            $where12 .= ‘ AND WmsOrderBase.house_id = ?‘;
            $where13 .= ‘ AND WmsOrderBase.house_id = ?‘;
            $where14.= ‘ AND WmsOrderBase.house_id = ?‘;
            array_push($params1, $houseID);
            array_push($params2, $houseID);
            array_push($params3, $houseID);
            array_push($params4, $houseID);
            array_push($params5, $houseID);
            array_push($params6, $houseID);
            array_push($params7, $houseID);
            array_push($params8, $houseID);
            array_push($params9, $houseID);
            array_push($params10, $houseID);
            array_push($params11, $houseID);
            array_push($params12, $houseID);
            array_push($params13, $houseID);
            array_push($params14, $houseID);
        }

        if ($startDate && $endDate) {
            $started = strtotime($startDate);
            $ended = strtotime($endDate);

            $where1 .= ‘ AND WmsBatchBase.created  <= ?‘;
            $where2 .= ‘ AND WmsBatchBase.created  <= ?‘;

            $where3 .= ‘ AND WmsBatchBase.created >= ? AND WmsBatchBase.created - (24*60*60) <= ? ‘;
            $where4 .= ‘ AND WmsBatchBase.created >= ? AND WmsBatchBase.created - (24*60*60) <= ? ‘;

            $where5 .= ‘ AND WmsBatchBase.created >= ? AND WmsBatchBase.created - (24*60*60) <= ? ‘;
            $where6 .= ‘ AND WmsBatchBase.created >= ? AND WmsBatchBase.created - (24*60*60) <= ? ‘;

            $where7 .= ‘ AND WmsBatchBase.created >= ? AND WmsBatchBase.created - (24*60*60) <= ? ‘;
            $where8 .= ‘ AND WmsBatchBase.created >= ? AND WmsBatchBase.created - (24*60*60) <= ? ‘;

            $where9 .= ‘ AND WmsOrderBase.created >= ? AND WmsOrderBase.created - (24*60*60) <= ? ‘;
            $where10 .= ‘ AND WmsOrderBase.created >= ? AND WmsOrderBase.created - (24*60*60) <= ? ‘;

            $where11 .= ‘ AND WmsOrderBase.created >= ? AND WmsOrderBase.created - (24*60*60) <= ? ‘;
            $where12 .= ‘ AND WmsOrderBase.created >= ? AND WmsOrderBase.created - (24*60*60) <= ? ‘;

            $where13 .= ‘ AND WmsOrderBase.created >= ? AND WmsOrderBase.created - (24*60*60) <= ? ‘;
            $where14 .= ‘ AND WmsOrderBase.created >= ? AND WmsOrderBase.created - (24*60*60) <= ? ‘;

            array_push($params1, $started);
            array_push($params2, $started);
            array_push($params3, $started, $ended);
            array_push($params4, $started, $ended);
            array_push($params5, $started, $ended);
            array_push($params6, $started, $ended);
            array_push($params7, $started, $ended);
            array_push($params8, $started, $ended);
            array_push($params9, $started, $ended);
            array_push($params10,$started, $ended);
            array_push($params11,$started, $ended);
            array_push($params12,$started, $ended);
            array_push($params13,$started, $ended);
            array_push($params14,$started, $ended);
        }
        $params =array_merge($params1,$params2,$params3,$params4,$params5,$params6,$params7,$params8,$params9,$params10,$params11,$params12,$params13,$params14);
        return array(
            $database->query(sprintf(self::ORDER_BATCH_ALL_REPORT_SQL, $where1, $where2, $where3, $where4, $where5, $where6, $where7, $where8, $where9, $where10, $where11, $where12,$where13,$where14, ($page - 1) * $size, $size), $params, DatabaseProvider::FETCH_STYLE_OBJ),
            $database->query(self::ORDER_BATCH_ALL_REPORT_COUNT_SQL),
        );
    }

  

时间: 2024-10-29 10:43:57

mysql语句集(一)的相关文章

15、sql语句集,Linux 下PHP查询mysql

一.mysql 创建数据库 mysql密码,默认没有 如果想更改的话, mysqladmin  -uroot   password  root123 登录   : [[email protected] root]#   mysql    -uroot    -proot123 1.创建book数据库 mysql>   create   database   book;Query OK, 1 row affected (0.08 sec) 2.授权给 lili 用户 mysql>  grant 

常用的MySql语句

/* 启动MySQL */net start mysql /* 连接与断开服务器 */mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL */mysqld --skip-grant-tables-- 修改root密码密码加密函数password()update mysql.user set password=password('root'); SHOW PROCESSLIST -- 显示哪些线程正在运行SHOW VARIABLES -- /* 数据库操

centos amoeba+mysql主从集群分离

原文来自:http://blog.163.com/na_llong/blog/static/113541609201352123232528/ 配置主从之前最好不要填写同步的数据库,默认都同步所有数据库就行. mysql 本机自带的,java 本机自带的. 1.登录Master服务器,修改my.cnf,添加如下内容: server-id = 1 //数据库ID号, 为1时表示为Master,其中master_id必须为1到232–1之间的一个正整数值; log-bin=mysql-bin //启

MySQL分布式集群之MyCAT(三)rule的分析【转】

首先写在最前面,MyCAT1.4的alpha版本已经发布了,这里面修复了不少的bug,也完善了一细节,之前两篇博客已经做了一些修改 ---------------------------------------------------------------------------------这才是本体~----------------------------------------------------------------------------------            之前已

基于MMM搭建MySQL Replication集群高可用架构

MMM介绍 MMM是Multi-Master Replication Manager for MySQL的缩写,它是MySQL提供的一个多主复制管理器,其核心是使用perl语言编写的一组脚本.实际上MMM是比较早期甚至有点老的一种用于构建高可用MySQL架构的方式,但因其还有一定的应用场景,所以本文将会演示一下如何搭建一个MMM架构. MMM 由两个组件组成: monitor:监控集群内数据库的状态,在出现异常时发布切换命令,一般和数据库分开部署 agent:运行在每个 MySQL 服务器上的代

总结今天学习的mysql语句

关键字 进入mysql:mysql -uroot -p 查看数据库:show databases 进入数据库:use DATABASE'S_NAME 查看数据库中表:show tables 以上在doc下演示,以后的在工具中演示 mysql中大小写不敏感 创建库:create database if not exists DATABASE'S_NAME 创建表:create table  if not exists  TABLE'S_NAME(列名1 属性,列名2 属性,......) 1 cr

在mysql语句中为什么要加反引号

在MySQL语句中我们有时候经常会遇到反引号(``),刚开始的时候不知道什么意思,他是什么作用呢? Select * from `member` order by posts desc limit 0,10; 它是为了区分MYSQL的保留字与普通字符而引入的符号. 举个例子:SELECT `select` FROM `test` WHERE select='字段值' 在test表中,有个select字段,如果不用反引号,MYSQL将把select视为保留字而导致出错,所以,有MYSQL保留字作为

【夯实Mysql基础】记一次mysql语句的优化过程!

1. [事件起因] 今天在做项目的时候,发现提供给客户端的接口时间很慢,达到了2秒多,我第一时间,抓了接口,看了运行的sql,发现就是 2个sql慢,分别占了1秒多. 一个sql是 链接了5个表同时使用了 2个 order by和 1个limit的分页 sql. 一个sql是上一个sql的count(*),即链接了5个表,当然没有limit了(取总数). 2. [着手优化] 1)[优化思路] 第一条是 做client调用 service层的数据缓存 第二条就是 优化sql本身. 这里着重讲一下

一些实用的mysql语句(不断积累更新)

1.数据表里仅仅有生日字段,想计算出其年龄的mysql语句: SELECT *,DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW( ))-TO_DAYS(生日字段)),'%Y')+0 AS age FROM 表名;