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 '7', 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 = '0'
  • 根据商品相关信息和已完成订单中该商品总成交量
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 ('194','195') OR gc.cat_id IN ('194','195') )
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 = 5 GROUP BY FROM_UNIXTIME(ADDTIME,'%Y-%m') LIMIT 0,10;

格式化时间戳: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;

 

posted on 2016-11-26 15:15  爱吃柠檬不加糖  阅读(286)  评论(0编辑  收藏  举报

导航