mysql执行语句汇总
- 插入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执行语句汇总的更多相关文章
- strace追踪mysql执行语句
一.strace参数 strace是Linux环境下的一款程序调试工具,用来监察一个应用程序所使用的系统调用及它所接收的系统信息.追踪程序运行时的整个生命周期,输出每一个系统调用的名字,参数,返回值和 ...
- linux strace追踪mysql执行语句 (mysqld --debug)
转载请注明出处:使用strace追踪多个进程 http://www.ttlsa.com/html/1841.html http://blog.itpub.net/26250550/viewspace- ...
- 8.mysql执行语句的顺序
mysql执行语句的顺序 一.group by + where group by 字句和where条件语句结合在一起使用,where在前,group by 在后.即先对select xx fr ...
- MySQL执行语句的顺序
MySQL的语句一共分为11步,最先执行的总是FROM操作,最后执行的是LIMIT操作.其中每一个操作都会产生一张虚拟的表,这个虚拟的表作为一个处理的输入,只是这些虚拟的表对用户来说是透明的,但是只有 ...
- 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 ...
- mysql 执行语句
连接数据库: $con = mysql_connect(服务器地址,用户名,密码): 选择数据库: $select = mysql_select_db(数据库名称); $select = mysql_ ...
- mysql执行语句提示Table 'performance_schema.session_variables' doesn't exist
用管理员身份cmd进入mysql安装目录bin里,执行 mysql_upgrade -u root -p 如果杀毒软件拦截,添加为信任区
- MySQL常用语句汇总--持续更新(2017-08-10)
修改表的字段结构: 表:mission_picture,新增字段:content,字段类型:text ALTER TABLE mission_picture ADD content text:
- Mysql执行计划说明
Mysql执行计划翻译: 官网原文请见http://dev.mysql.com/doc/refman/5.6/en/explain-output.html:5.6 EXPLAIN语句提供有关SELEC ...
随机推荐
- vs2010中设置qt环境的智能识别方案
Qt搭建请参考:Win7系统VS2010下搭建qt开发环境 搭建好之后,虽然可以编译过去,但是写代码时,编辑器无法识别,也没有智能提示,并且代码中都是红色的提示如下: 此时需要设置一下include路 ...
- apache-ab并发负载压力测试 不错
ab -n 3000 -c 3000 http://www.test.com/ c 100 即:每次并发3000 个 n 10000 即: 共发送3000 个请求 ab -t 60 -c 100 ht ...
- 逆向分析一个完整的C++程序包含寄存器与参数传递详解
最近在分析C++ dump 文件的时候觉得有必要将一些必要的反汇编东西总结一下以备别人参考,自己有时间的时候也可以进行更多的改进.下面通过一个简单的C++代码转成汇编代码后的详细解释说明一下C++和汇 ...
- bzoj3631 [JLOI2014]松鼠的新家——树上差分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3631 树上差分:注意路径的结尾被多算了一次,最后要减去(不能提前减). 代码如下: #inc ...
- [转]如何用git将项目代码上传到github
注册账户以及创建仓库 要想使用github第一步当然是注册github账号了.之后就可以创建仓库了(免费用户只能建公共仓库),Create a New Repository,填好名称后Create,之 ...
- UI: 网易新闻实现
#pragma mark-(AppDelegate.H文件)-------------------------------------------------------------------- ...
- 06_传智播客iOS视频教程_源文件后缀名和main函数
OC与C语言的不同. 把OC程序运行起来,CPU只会找main函数.并且只会执行main函数当中的代码.当main函数的代码执行完毕之后这个程序就自动结束掉了. main函数的参数是可以让我们在运行程 ...
- Pow(x, n) 位运算总结 典型
https://leetcode.com/problems/powx-n/ Implement pow(x, n), which calculates x raised to the power n ...
- 使用root用户登录到AWS EC2服务器,上传文件到/var/www目录
关键词 1.aws ec2中上传文件到/var/www目录(使用filezilla) 2.使用root用户登录aws ec2实例 上一篇随笔中记录了在aws ec2实例中部署apache服务器的过程, ...
- Codeforces 938D Buy a Ticket 【spfa优化】
用到了网络流的思想(大概).新建一个源点s,所有边权扩大两倍,然后所有的点向s连边权为点权的无向边,然后以s为起点跑spfa(S什么L优化的),这样每个点到s的距离就是答案. 原因的话,考虑答案应该是 ...