1,字符串截取拼接

CONCAT(),'****');
SUBSTRING_INDEX(c.context,'}',1);
SUBSTRING_INDEX(a.task_context,':',-1) as context;

2,Case函数

(
        CASE a.type_code
        WHEN 'bp' THEN
            '血压'
        WHEN 'hr' THEN
            '心率'
        WHEN 'fbg' THEN
            '空腹血糖'
        WHEN '2hpbg' THEN
            '餐后两小时血糖'
        WHEN 'tc' THEN
            '总胆固醇'
        WHEN 'bua' THEN
            '血尿酸'
        WHEN 'bmi' THEN
            '体质指数'
        ELSE
            '其他'
        END
    ) AS type_code

3,字符串拼接

CONCAT_WS('/',b.value1,b.value2,b.value3);
CONCAT(a.value1,'mmol/L') AS value1

4,日期加减

DATEDIFF(DATE(MAX(take_time)),DATE(MIN(take_time))) as days

5,年龄计算

YEAR (NOW()) - YEAR (b.birthday) AS birthday

6,IF函数

,'女','男') AS gender

7,COUNT函数

)

8,外联

SELECT
    f.id,
    f.store_name,
    f.vendor_name,
    f.area_name,
    f.store_phone,
    h.realname,
    h.mobile,
    f.created_time
FROM
    (
        SELECT
            a.id,
            a.store_name,
            c.vendor_name,
            b.area_name,
            a.store_phone,
            a.created_time
        FROM
            pd_store a,
            pd_vendor_area b,
            pd_vendor c
        WHERE
            a.vendor_area_id = b.id
        AND a.vendor_id = c.id

        AND b.area_name LIKE '%%'
        LIMIT ,
    ) f
LEFT OUTER JOIN pd_clerk g ON f.id = g.store_id

LEFT OUTER JOIN pd_user h ON g.user_id = h.uid

9, 外联

SELECT
    k.operater_id,
    k.realname,
    k.mobile,
    k.pdNum,
    l.operater_id,
    l.realname,
    l.mobile,
    l.dslNum
FROM
    (
        SELECT
            a.operater_id,
            b.realname,
            b.mobile,
            COUNT(DISTINCT a.user_id) AS pdNum
        FROM
            pd_indicator_values a,
            pd_user b,
            pd_user_vendor c
        WHERE
            a.operater_id = b.uid

        AND a.type_code <> 'hr'
        AND a.user_id = c.user_id
        AND a.take_time >= '2016-10-26 00:00:00'
        AND a.take_time <= '2016-11-30 23:59:59'

        AND c.vendor_member_id LIKE '202%'
        GROUP BY
            a.operater_id
    ) k
LEFT OUTER JOIN (
    SELECT
        a.operater_id,
        b.realname,
        b.mobile,
        COUNT(DISTINCT a.user_id) AS dslNum
    FROM
        pd_indicator_values a,
        pd_user b,
        pd_user_vendor c
    WHERE
        a.operater_id = b.uid

    AND a.type_code <> 'hr'
    AND a.user_id = c.user_id
    AND a.take_time >= '2016-10-26 00:00:00'
    AND a.take_time <= '2016-11-30 23:59:59'

    AND c.vendor_member_id NOT LIKE '202%'
    GROUP BY
        a.operater_id
) l ON k.operater_id = l.operater_id
UNION
    SELECT
        k.operater_id,
        k.realname,
        k.mobile,
        k.pdNum,
        l.operater_id,
        l.realname,
        l.mobile,
        l.dslNum
    FROM
        (
            SELECT
                a.operater_id,
                b.realname,
                b.mobile,
                COUNT(DISTINCT a.user_id) AS pdNum
            FROM
                pd_indicator_values a,
                pd_user b,
                pd_user_vendor c
            WHERE
                a.operater_id = b.uid

            AND a.type_code <> 'hr'
            AND a.user_id = c.user_id
            AND a.take_time >= '2016-10-26 00:00:00'
            AND a.take_time <= '2016-11-30 23:59:59'

            AND c.vendor_member_id LIKE '202%'
            GROUP BY
                a.operater_id
        ) k
    RIGHT OUTER JOIN (
        SELECT
            a.operater_id,
            b.realname,
            b.mobile,
            COUNT(DISTINCT a.user_id) AS dslNum
        FROM
            pd_indicator_values a,
            pd_user b,
            pd_user_vendor c
        WHERE
            a.operater_id = b.uid

        AND a.type_code <> 'hr'
        AND a.user_id = c.user_id
        AND a.take_time >= '2016-10-26 00:00:00'
        AND a.take_time <= '2016-11-30 23:59:59'

        AND c.vendor_member_id NOT LIKE '202%'
        GROUP BY
            a.operater_id
    ) l ON k.operater_id = l.operater_id;

N,其他

SELECT
  b.store_name,
  a.take_time,
  c.realname,
  CONCAT(),'****'),
  c.mobile,
  (
        CASE a.type_code
        WHEN 'bp' THEN
            '血压'
        WHEN 'hr' THEN
            '心率'
        WHEN 'fbg' THEN
            '空腹血糖'
        WHEN '2hpbg' THEN
            '餐后两小时血糖'
        WHEN 'tc' THEN
            '总胆固醇'
        WHEN 'bua' THEN
            '血尿酸'
        WHEN 'bmi' THEN
            '体质指数'
        ELSE
            '其他'
        END
    ) AS type_code,
  a.value1,
  a.value2,
  a.value3,
  d.realname as clerkname,
  d.mobile as clerkmobile
FROM
    pd_indicator_values a,
    pd_store b,
    pd_user c,
    pd_user d
WHERE
    a.store_id = b.id
AND a.user_id = c.uid
AND a.operater_id = d.uid

AND a.take_time >= '2016-09-01 00:00:00'
AND a.take_time < '2016-11-01 00:00:00';
SELECT
    a.realname,
  a.gender,
  c.realname as clerkname,
  c.mobile,
  d.store_name,
  b.type_code,
  b.take_time,
  CONCAT_WS('/',b.value1,b.value2,b.value3)
FROM
    pd_user a,
    pd_indicator_values b,
    pd_user c,
    pd_store d
WHERE
    a.uid = b.user_id
AND b.operater_id = c.uid
AND b.store_id = d.id ORDER BY b.user_id ASC;
SELECT
    COUNT(*)
FROM
    (
        SELECT
            user_id,
            COUNT(user_id) AS coun,
            DATEDIFF(
                DATE(MAX(take_time)),
                DATE(MIN(take_time))
            ) AS days
        FROM
            pd_indicator_values
        WHERE
            take_time >= '2015-11-01 00:00:00'
        AND take_time <= '2016-11-24 23:59:59'

        GROUP BY
            user_id
        HAVING
            coun 

    ) k;
SELECT
    h.vendor_name,
    h.take_time,
    h.realname,
    h.created_time,
    h.gender,
    h.birthday,
    h.address,
    h.mobile,
    h.id_card,
    h.type_code,
    h.value1_status,
    h.value1,
    h.value2_status,
    h.value2,
    h.value3_status,
    h.value3,
    h.clerkname,
    h.clerkmobile,
    h.store_name,
    j.realname as stoname,
    j.mobile as stomobile
FROM
    (
        SELECT
            e.vendor_name,
            a.take_time,
            f.created_time,
            b.realname,
            (
                CASE b.gender
                ' THEN
                    '女'
                ' THEN
                    '男'
                ELSE
                    ' '
                END
            ) AS gender,
            YEAR (NOW()) - YEAR (b.birthday) AS birthday,
            g.address,
            b.mobile,
            b.id_card,
            (
                CASE a.type_code
                WHEN 'bp' THEN
                    '血压'
                WHEN 'hr' THEN
                    '心率'
                WHEN 'fbg' THEN
                    '空腹血糖'
                WHEN '2hpbg' THEN
                    '餐后两小时血糖'
                WHEN 'tc' THEN
                    '总胆固醇'
                WHEN 'bua' THEN
                    '血尿酸'
                WHEN 'bmi' THEN
                    '体质指数'
                ELSE
                    '其他'
                END
            ) AS type_code,
            (
                CASE a.value1_status
                ' THEN
                    '正常'
                ' THEN
                    '风险'
                ' THEN
                    '危险'
                ' THEN
                    '偏小风险'
                ' THEN
                    '偏小危险'
                ELSE
                    '其他'
                END
            ) AS value1_status,
            a.value1,
            (
                CASE a.value2_status
                ' THEN
                    '正常'
                ' THEN
                    '风险'
                ' THEN
                    '危险'
                ' THEN
                    '偏小风险'
                ' THEN
                    '偏小危险'
                ELSE
                    '其他'
                END
            ) AS value2_status,
            a.value2,
            (
                CASE a.value3_status
                ' THEN
                    '正常'
                ' THEN
                    '风险'
                ' THEN
                    '危险'
                ' THEN
                    '偏小风险'
                ' THEN
                    '偏小危险'
                ELSE
                    '其他'
                END
            ) AS value3_status,
            a.value3,
            c.realname AS clerkname,
            c.mobile AS clerkmobile,
            d.store_name,
            d.id,
      q.cou
        FROM
            (
                SELECT
                    o.user_id,
                    COUNT(o.user_id) AS cou
                FROM
                    pd_indicator_values o
                WHERE
                    o.take_time >= '2015-06-01 00:00:00'
                AND o.take_time <= '2016-11-22 23:59:59'
                GROUP BY
                    o.user_id
                HAVING
                    cou
            ) q,
            pd_indicator_values a,
            pd_user b,
            pd_user c,
            pd_store d,
            pd_vendor e,
            pd_user_vendor f,
            pd_user_info g
        WHERE
            q.user_id = a.user_id
        AND a.user_id = b.uid
        AND a.operater_id = c.uid
        AND a.store_id = d.id
        AND a.vendor_id = e.id
        AND a.user_id = f.user_id
        AND a.user_id = g.user_id
        AND a.take_time >= '2015-06-01 00:00:00'
        AND a.take_time <= '2016-11-22 23:59:59'
    ) h,
    pd_clerk i,
    pd_user j
WHERE
    h.id = i.store_id

AND i.user_id = j.uid ORDER BY h.cou DESC;

1,将查询结果存入表
insert into pd_temp select * from pd_other;

2,将表数据导出xls文件最大1048576记录数
select * from pd_temp limit 1048576,364387 into outfile 'c:\\20.xls';
3,格式转换
记事本另存为ASCII格式

iconv -futf8 -tgb2312 -otest 21.xls 20.xls

SELECT
    a.take_time AS take_time,
    b.realname AS realname,
    ,'女','男') AS gender,
    b.mobile AS mobile,
    b.id_card AS id_card,
    IF(a.type_code='fbg', '空腹血糖', '餐后两小时血糖') AS type_code,
    CONCAT(a.value1,'mmol/L') AS value1,
    d.realname as clerkname,
  d.mobile as clerkmobile,
    c.store_name AS store_name
FROM
    pd_indicator_values a,
    pd_user b,
    pd_store c,
    pd_user d
WHERE
    a.user_id = b.uid
AND a.operater_id = d.uid
AND a.store_id = c.id

AND a.take_time >= '2016-08-01 00:00:00'
AND a.take_time < '2016-09-01 00:00:00'
AND (
    a.type_code = 'fbg'
    OR a.type_code = '2hpbg'
)
AND (
    a.value1_status 

);
SELECT
    d.id,
    d.store_name,
    c.realname,
    c.mobile,
    count(a.caller_user_id),
    count(

    ),
    ),
    SUM(a.caller_duration)
FROM
    pd_call_records a,
    pd_clerk b,
    pd_user c,
    pd_store d
WHERE
    a.caller_user_id = b.user_id
AND b.user_id = c.uid
AND b.store_id = d.id
AND a.created_time >= '2016-08-01 18:48:45'
GROUP BY
    a.caller_user_id;
SELECT
    c.realname,
    c.mobile,
    SUBSTRING_INDEX(c.context, ),
    c.callednum,
    c.callnum,
    c.calltime,
    d.realname,
    f.store_name,
    d.mobile
FROM
    (
        SELECT
            b.realname AS realname,
            b.mobile AS mobile,
            SUBSTRING_INDEX(a.task_context, ) AS context,

        , , ) AS callednum,

    , , ) AS callnum,
    IFNULL(a.called_duration, ) AS calltime,
    a.caller_user_id AS clerkid
FROM
    pd_call_records a,
    pd_user b
WHERE
    a.called_user_id = b.uid

    ) c,
    pd_user d,
    pd_clerk e,
    pd_store f
WHERE
    c.clerkid = d.uid
AND d.uid = e.user_id
AND e.store_id = f.id;
SELECT COUNT(k.user_id) from (
SELECT
  a.user_id,
    DATEDIFF(DATE(MAX(a.take_time)),DATE(MIN(a.take_time))) as days
FROM
    pd_indicator_values a
WHERE
    a.vendor_id
AND a.take_time >= '2016-10-26 00:00:00'
AND a.take_time <= '2016-11-30 23:59:59'
GROUP BY a.user_id

)k 

SQL函数的更多相关文章

  1. Oracle 中的sql函数以及分页

    SELECT LPAD(,'*.') "LPAD example" FROM DUAL; 1.分页查询 (1)方法一:使用  between  and 来实现分页 select * ...

  2. SQL函数说明大全

    一旦成功地从表中检索出数据,就需要进一步操纵这些数据,以获得有用或有意义的结果.这些要求包括:执行计算与数学运算.转换数据.解析数值.组合值和聚合一个范围内的值等. 下表给出了T-SQL函数的类别和描 ...

  3. oracle(sql)基础篇系列(一)&mdash;&mdash;基础select语句、常用sql函数、组函数、分组函数

        花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用ora ...

  4. [转]字符型IP地址转换成数字IP的SQL函数

    使用SQL函数可以实现许多的功能,下面为您介绍的是字符型IP地址转换成数字IP的SQL函数示例,供您参考,希望对您学习SQL函数能够有所帮助.      /**//*--调用示例       sele ...

  5. 常用的Sql 函数

    常用的Sql 函数 1: replace 函数,替换字符. 语法 replace (original-string, search-string, replace-string ) 第一个参数你的字符 ...

  6. 常用的 SQL 函数

    SQL 函数 聚合函数(针对数字列): AVG:求平均分  COINT: 计算个数  MAX: 求最大值  MIN: 求最小值  SUM: 求和 数学函数():  ABS:     绝对值  CEIL ...

  7. Oracle数据库--SQL函数

    Oracle SQL函数  1.ASCII返回与指定的字符对应的十进制数;SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii( ...

  8. 常用的sql函数

    常用的sql函数 concat('hello','world') 结果:helloworld  作用:拼接 substr('helloworld',1,5)      hello           ...

  9. ThinkPHP使用SQL函数进行查询

    //SQL函数查询 $products=$pro->where(array("FIND_IN_SET('".$type."',type)",'num'=& ...

  10. oracle PL/SQL(procedure language/SQL)程序设计(续集)之PL/SQL函数

    PL/SQL函数 examples:“ 构造一个邮件地址 v_mailing_address := v_name||CHR(10)||                                 ...

随机推荐

  1. github常见操作和常见错误!错误提示:fatal: remote origin already exists.

    如果输入$ git remote add origin git@github.com:djqiang(github帐号名)/gitdemo(项目名).git 提示出错信息:fatal: remote ...

  2. SpringQtz 时间任务调度

    1.配置所需要maven jar包 <!-- 任务调度需要的jar包--> <dependency> <groupId>org.quartz-scheduler&l ...

  3. linux 定时器编程实例(完善中).....

    最近在写linux 下的定时器编程实验,测试发现 usleep函数在 x86 架构下的定时还是比较准确的,在arm9下 就不太准了. 今天用linux 下的setitimer()函数进行了定时 器的测 ...

  4. Spark环境的搭建与运行

    Spark本地安装与配置 下载spark后解压,并cd到解压目录下 运行实例程序测试是否一切正常 ./bin/run-example org.apache.spark.examples.SparkPi ...

  5. 转载 How to Encrypt connection string in web.config

    转载原地址: https://chiragrdarji.wordpress.com/2008/08/11/how-to-encrypt-connection-string-in-webconfig/ ...

  6. C++“窗体”程序设计启蒙

    [摘要]本文以C++菜菜鸟(仅仅须要学习了C++数据类型和控制结构就可以)为目标读者,用求解一元二次方程作为实例,展示窗体式程序的开发过程,获得初步体验.写作目的包含:(1)让学生通过模仿,开发出类似 ...

  7. 自制Linux重命名命令

    相比于Windows上的ren命名,Linux还真的是没有一个特定的重命名的命令.(虽然可以间接的使用mv来实现).下面我就来自己写一个简单的重命名命令. 准备工作 操作系统: Linux内核的系统都 ...

  8. 安装graphlab伤透了心,终于搞定了

    为了方便研究各种机器学习算法,我想用graphlab来辅助我对后续算法的研究.所以我的目标就是安装graphlab到我的windows笔记本中.而基于python的graphlab的安装最好是采用如下 ...

  9. entity framework浅谈

    1. 什么是EF 微软提供的ORM工具. ORM让开发人员节省数据库访问代码的时间. 将更多的时间放在业务逻辑层面上. 开发人员使用linq语言, 对数据库进行操作. 2. EF的使用场景 EF有三种 ...

  10. Permission denied (publickey),Gitlab &amp; Github 多ssh key 冲突 导致的权限问题

    Github 多ssh key导致的权限问题 :Permission denied (publickey) 公司用gitlib搭建了git服务器,自己已有github账号,用ssh-keygen分别生 ...