常用SQL

##2016年11月24日 group by 分组,如distinct.

select * from mch_msg_order group by order_id  

结果集是:按照id asc排序,筛选出记录

SELECT * FROM (SELECT * FROM `mch_msg_order` ORDER BY `to_status` DESC) t GROUP BY `order_id` ORDER BY `to_status` DESC

按照to_status排序,在结果集中分组,即可获取to_status最大的不重复的记录

 

##2016年9月20日  两个字段相加

select id,(sale+virtual_sale) as c from mch_wechat_product_info

##2016-07-21 对于导出excel为科学计数法的数据,使用concat转化

SELECT u.`realname` ,u.`phone` ,concat('订单',g.`wxnumber`) as wx, g.number FROM `wm_barter_goods` as g left join wm_barter_users as u on g.usersId = u.id WHERE g.`cTime` > 1468944000 

##2016-04-01 常用sql

alter table wm_m_wechat_list add constraint UQ_openid UNIQUE(openid)

##2016-03-16  选择数量大于1的数据

select * ,count(id) as num from wm_m_wechat_list GROUP BY openid HAVING num>1

 

posted on 2015-12-26 13:08  y_php  阅读(164)  评论(0编辑  收藏  举报