• 插入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. CSS的两个class选择器紧挨在一起

    有一段HTML代码: <a class="glyphicons white no-js cogwheel" href="#" target="_ ...

  2. HDU3394 Railway —— 点双联通分量 + 桥(割边)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3394 Railway Time Limit: 2000/1000 MS (Java/Others)   ...

  3. beyond compare 比较文本 standard alignment VS unaligned

    在Rules里面 Standard Alignment 这种方式会比较找出相同的部分,可能会跨行找相同的 Unaligned 这种比较直接每一行之间相互比较,不跨行找相同的

  4. Android中onInterceptTouchEvent、dispatchTouchEvent及onTouchEvent的调用顺序及内部原理

    在Android中需要经常对用户手势进行判断,在判断手势时需要精细的分清楚每个触摸事件以及每个View对事件的接收情况,在View,ViewGroup,Activity中都可以接收事件,在对事件进行处 ...

  5. fiddler工具使用

    一.安装 a) 百度fiddler ,下载, 安装 ,无脑流 二.界面介绍 a) 工具栏与状态栏 其中保存是,可以为两种形式:1.text文本形式 2.saz文件结尾数据(能被fiddler软件识别) ...

  6. bzoj 1613: [Usaco2008 Jan]Running贝茜的晨练计划【dp】

    设f[i][j]为第i分钟疲劳j,从三种情况转移,记得休息的时候判断从i开始休息到n能不能恢复到疲劳0 #include<iostream> #include<cstdio> ...

  7. P3171 [CQOI2015]网络吞吐量

    传送门 首先跑一遍最短路,如果一条边满足\(dis[v]=dis[u]+w[i]\),那么这条边就在最短路中,把它加进网络流的图里 然后点的流量限制的话拆点,把每个点拆成两个,中间连边来限制流量 最后 ...

  8. linux 安装和远程连接

    准备工作: 1.请安装好vmware 软件 2.linux 镜像包 3.putty 远程连接工具 任务: 设置好root 账号和普通账号 及设置网络 连接最简单使用桥接 只能ping 通 本机 nat ...

  9. quickpow || 快速幂

    洛谷例题 推荐自行脑补:百度百科 如果  ,那么 : 前言:快速幂就是快速算底数的n次幂.其时间复杂度为 O(log₂N), 与朴素的O(N)相比效率有了极大的提高. 拿题目样例 Input :2 1 ...

  10. linux 重名名、删除文件操作

    linux下重命名文件或文件夹的命令mv既可以重命名,又可以移动文件或文件夹. 例子:将目录A重命名为B mv A B 例子:将/a目录移动到/b下,并重命名为c mv /a /b/c 删除文件夹 r ...