• 插入select的数据
INSERT INTO `test1`( order_id, goods_id, goods_name, goods_sn, product_id, goods_number, market_price, goods_price, goods_attr, is_real, extension_code, parent_id, is_gift, goods_attr_id)
SELECT '', goods_id, goods_name, goods_sn, product_id, goods_number, market_price, goods_price, goods_attr, is_real, extension_code, parent_id, is_gift, goods_attr_id
FROM `test2` WHERE session_id = '915f0eed888dc48be674a492152d76b0' AND rec_type = ''
  • 根据商品相关信息和已完成订单中该商品总成交量
SELECT eg.goods_img_02,eg.goods_id,SUM(eog.goods_number) AS good_number,eog.`order_id`
FROM g1 AS eg
LEFT JOIN g2 AS eog
ON eg.`goods_id`=eog.`goods_id` AND eog.order_id IN(
SELECT order_id FROM g3 AS eoi
WHERE eoi.order_status=5
AND eoi.shipping_status=2
AND eoi.pay_status=2
)
WHERE eg.is_hot=1 AND eg.is_on_sale=1
AND eg.is_delete=0
GROUP BY eg.goods_id;
  • 获得指定分类下的品牌(有商品的)
SELECT b.brand_id, b.brand_name, COUNT(*) AS goods_num
FROM brand AS b, goods AS g
LEFT JOIN goods_cat AS gc
ON g.goods_id = gc.goods_id
WHERE g.brand_id = b.brand_id
AND (g.cat_id IN ('','') OR gc.cat_id IN ('','') )
AND b.is_show = 1 AND g.is_on_sale = 1 AND g.is_alone_sale = 1
AND g.is_delete = 0
GROUP BY b.brand_id HAVING goods_num > 0
ORDER BY b.sort_order, b.brand_id ASC
  • 根据多值分组(将查询出来的数据作为表继续筛选查询)
SELECT * FROM (SELECT * FROM (
SELECT eg.goods_id, eg.goods_name, eg.goods_img_02, eg.shop_price , SUM(eog.`goods_number`) AS order_goods_num, eg.good_type, ega.`act_type`, eg.`bind_id`, eg.is_new, eg.`add_time`
FROM g AS eg
LEFT JOIN h AS eog
ON eg.goods_id=eog.goods_id
LEFT JOIN j AS eoi
ON eoi.order_id=eog.`order_id`
AND eoi.order_status=5 AND eoi.shipping_status=2 AND eoi.pay_status=2
LEFT JOIN g_activity AS ega
ON ega.goods_id = eg.`goods_id` AND ega.is_finished=1
WHERE
eg.`is_delete`=0 AND eg.is_on_sale=1 AND eg.shop_price >= 1 AND eg.shop_price <= 999999 AND eg.`bind_id`>0 GROUP BY eg.goods_id
) AS bind1 GROUP BY bind_id UNION SELECT * FROM (
SELECT eg.goods_id, eg.goods_name, eg.goods_img_02, eg.shop_price , SUM(eog.`goods_number`) AS order_goods_num, eg.good_type, ega.`act_type`, eg.`bind_id`, eg.is_new, eg.`add_time`
FROM g AS eg
LEFT JOIN h AS eog
ON eg.goods_id=eog.goods_id
LEFT JOIN j AS eoi
ON eoi.order_id=eog.`order_id`
AND eoi.order_status=5 AND eoi.shipping_status=2 AND eoi.pay_status=2
LEFT JOIN g_activity AS ega
ON ega.goods_id = eg.`goods_id` AND ega.is_finished=1
WHERE
eg.`is_delete`=0 AND eg.is_on_sale=1 AND eg.shop_price >= 1 AND eg.shop_price <= 999999 AND eg.`bind_id`=0 GROUP BY eg.goods_id
) AS bind2) AS goods ORDER BY is_new DESC , add_time DESC LIMIT 0,600
  • 更改字段类型/描述
//就算只更改字段描述,其他字段信息(如类型等)也都得写出,否则,报错或者按照默认类型进行更改
ALTER TABLE talbel_a MODIFY COLUMN good_type TINYINT(1) DEFAULT 1 COMMENT '描述信息';
  •  联合查询,去重
//union 联合查询
//注意事项:
//1·每个表查询前加上表别名 2·联合查询字段类型要一致
select auth_union.* from (
select u1.t_id as account_id,u1.user_name_auth as account_name,u2.t_id as role_id,u2.role_name as role_name , u1.user_time_create
from dyw_user as u1 left join dyw_role as u2 on u1.fk_role_id=u2.t_id
where u2.fk_shop_id=31 and u2.role_name!='super_admin' and u1.user_del_flag=0 and u1.user_type=13
union
select u3.t_id as account_id ,u3.user_name_auth as account_name, 0 as role_id,'',u3.user_time_create
from dyw_user u3 where u3.t_id in(select u4.fk_user_id from dyw_r_user_shop u4 where u4.fk_shop_id=31) and u3.user_del_flag=0 and u3.user_type=13
) as auth_union group by auth_union.account_name order by auth_union.account_name desc limit 0,20 //sql 去重复
//查询总数去重复 关键字 distinct
select count(distinct auth_union.account_id) from (
select u1.t_id as account_id,u1.user_name_auth as account_name,u2.t_id as role_id,u2.role_name as role_name , u1.user_time_create
from dyw_user as u1 left join dyw_role as u2 on u1.fk_role_id=u2.t_id
where u2.fk_shop_id=? and u2.role_name!='super_admin' and u1.user_del_flag=0 and u1.user_type=?
union
select u3.t_id as account_id ,u3.user_name_auth as account_name, 0 as role_id,'',u3.user_time_create
from dyw_user u3 where u3.t_id in(select u4.fk_user_id from dyw_r_user_shop u4 where u4.fk_shop_id=?) and u3.user_del_flag=0 and u3.user_type=?
) as auth_union //查询数据去重复 关键字 group by
group by auth_union.account_name
  •  查询以‘dsc_’的所有表组成批量删除语句
SELECT CONCAT( 'drop table ', table_name, ';' )
FROM information_schema.tables
WHERE table_name LIKE 'dsc_%' GROUP BY table_name;
  •  查询以月份统计总额
SELECT SUM(money) AS moneyMonthTotal,FROM_UNIXTIME(ADDTIME,'%Y-%m') AS `month` FROM A WHERE STATUS =  GROUP BY FROM_UNIXTIME(ADDTIME,'%Y-%m') LIMIT ,;

格式化时间戳:FROM_UNIXTIME  DATE_FORMAT 

转为时间戳: UNIX_TIMESTAMP

格式化参考地址:http://www.w3school.com.cn/sql/func_date_format.asp  

SELECT *,FROM_UNIXTIME(UNIX_TIMESTAMP(pd.`timeday`), '%Y-%m-%d' ) AS post_date
FROM wxapi_performance_day AS pd
WHERE pd.`AppID`=5211395 AND pd.`uid`=10004339 AND pd.`cronid`=2094
ORDER BY timeday DESC;

mysql执行语句汇总的更多相关文章

  1. strace追踪mysql执行语句

    一.strace参数 strace是Linux环境下的一款程序调试工具,用来监察一个应用程序所使用的系统调用及它所接收的系统信息.追踪程序运行时的整个生命周期,输出每一个系统调用的名字,参数,返回值和 ...

  2. linux strace追踪mysql执行语句 (mysqld --debug)

    转载请注明出处:使用strace追踪多个进程 http://www.ttlsa.com/html/1841.html http://blog.itpub.net/26250550/viewspace- ...

  3. 8.mysql执行语句的顺序

    mysql执行语句的顺序     一.group by + where group by 字句和where条件语句结合在一起使用,where在前,group by 在后.即先对select xx fr ...

  4. MySQL执行语句的顺序

    MySQL的语句一共分为11步,最先执行的总是FROM操作,最后执行的是LIMIT操作.其中每一个操作都会产生一张虚拟的表,这个虚拟的表作为一个处理的输入,只是这些虚拟的表对用户来说是透明的,但是只有 ...

  5. MySql 执行语句错误 Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

    关于用Power Designer 生成sql文件出现 错误  [Err] 1064 - You have an error in your SQL syntax; check the manual ...

  6. mysql 执行语句

    连接数据库: $con = mysql_connect(服务器地址,用户名,密码): 选择数据库: $select = mysql_select_db(数据库名称); $select = mysql_ ...

  7. mysql执行语句提示Table 'performance_schema.session_variables' doesn't exist

    用管理员身份cmd进入mysql安装目录bin里,执行 mysql_upgrade -u root -p 如果杀毒软件拦截,添加为信任区

  8. MySQL常用语句汇总--持续更新(2017-08-10)

    修改表的字段结构: 表:mission_picture,新增字段:content,字段类型:text ALTER TABLE mission_picture ADD content text:

  9. Mysql执行计划说明

    Mysql执行计划翻译: 官网原文请见http://dev.mysql.com/doc/refman/5.6/en/explain-output.html:5.6 EXPLAIN语句提供有关SELEC ...

随机推荐

  1. 前端富文本 js 版本

    https://s3.pstatp.com/pgc/v2/resource/tt_ueditor_v3_temple/tt-editor.all.js?20180425

  2. ABAP JSON

    1 DATA: lv_em TYPE string, lv_em1 TYPE string. DATA: lt_but021 TYPE TABLE OF but021, lt_but0211 TYPE ...

  3. 《CMake实践》笔记二:INSTALL/CMAKE_INSTALL_PREFIX【转】

    本文转载自:http://www.cnblogs.com/52php/p/5681751.html 四.更好一点的Hello World 没有最好,只有更好 从本小节开始,后面所有的构建我们都将采用  ...

  4. HDU2389 Rain on your Parade —— 二分图最大匹配 HK算法

    题目链接:https://vjudge.net/problem/HDU-2389 Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)  ...

  5. hdu-4118 Holiday's Accommodation(树形dp+树的重心)

    题目链接: Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others)     Memory Limit: 200000/200000 ...

  6. Java-Runoob-高级教程-实例-字符串:12. Java 实例 - 字符串优化

    ylbtech-Java-Runoob-高级教程-实例-字符串:12. Java 实例 - 字符串优化 1.返回顶部 1. Java 实例 - 字符串优化  Java 实例 以下实例演示了通过 Str ...

  7. bzoj4720

    期望dp n久以前做过,再做一遍 你只能决定决策,不能决定结果,这是这道题的关键,因为我们换了教室不一定成功,所以我们应该这样设dp状态,dp[i][j][k],第i天,换j次,换没换,转移: dp[ ...

  8. Git dev分支合并到master分支完美实战

    aa@aa-PC MINGW64 /f/online-center/cloud/server-functions_services (master)                          ...

  9. 【优化算法】Greedy Randomized Adaptive Search算法 超详细解析,附代码实现TSP问题求解

    01 概述 Greedy Randomized Adaptive Search,贪婪随机自适应搜索(GRAS),是组合优化问题中的多起点元启发式算法,在算法的每次迭代中,主要由两个阶段组成:构造(co ...

  10. 关于新手html的认识 以及对table的基本用法

    1.html语言 <!DOCTYPE html> 声明html <!--双标签--> <!--<html 属性名="属性值 " 属性名2=&qu ...