• 插入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. spring依赖注入(反转控制)

    SPRING依赖注入机制(反转控制)解析 Spring能有效地组织J2EE应用各层的对象.不管是控制层的Action对象,还是业务层的 Service对象,还是持久层的DAO对象,都可在Spring的 ...

  2. Class.forName() 详解

    主要功能 Class.forName(xxx.xx.xx)返回的是一个类 Class.forName(xxx.xx.xx)的作用是要求JVM查找并加载指定的类, 也就是说JVM会执行该类的静态代码段 ...

  3. linux怎么区别文本文件和二进制文件

    linux的文本文件与二进制文件的区分与windows的区分是相同的!说到底计算机存储的文件都是以二进制形式存储的,但是区别是,习惯上认为: (1).文本文件 文本文件是包含用户可读信息的文件.这些文 ...

  4. HDU1754 —— I Hate It 线段树 单点修改及区间最大值

    题目链接:https://vjudge.net/problem/HDU-1754 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜 ...

  5. problem in Sourcetree

    1.The date is commit date not the date of author 2.The log line is ordered  by time, actually it sho ...

  6. HDU - 1150 Machine Schedule(最小点覆盖数)

    1.有两台机器A和B以及N个需要运行的任务.A机器有n种不同的模式,B机器有m种不同的模式,而每个任务都恰好在一台机器上运行.如果它在机器A上运行,则机器A需要设置为模式xi,如果它在机器B上运行,则 ...

  7. HTTP传输二进制初探

    [转]HTTP传输二进制初探 http://www.51testing.com/?uid-390472-action-viewspace-itemid-233993 [转]HTTP传输二进制初探 上一 ...

  8. android:json解析的两个工具:Gson和Jackson的使用小例子

    1.简介 json是android与服务器通信过程中常用的数据格式,例如,如下是一个json格式的字符串: {"address":"Nanjing"," ...

  9. linux CANopenSocket 初试

    /************************************************************************************** * linux CANo ...

  10. 并不对劲的图论专题(三):SPFA算法的优化

    1.bzoj1489-> 这是个新套路. 我们希望找到最小的x,那么可以二分x,然后判断是否存在圈的边权的平均值小于等于x. 设圈的边权依次为w1,w2,w3,…,wk,平均值为p, 则有p= ...