多个字段唯一性如何处理?
有张表,表里有多个字段需要唯一,不能重复,否则就是重复数据,插不进去
直接给多个字段直接加唯一索引,简单粗暴
新增一个字段,这个字段加唯一索引,这样可以不用加过多唯一索引
insert into news(new_title, new_abstr, new_code)
values('你是好','我是谁',MD5(CONCAT('你是好','我是谁')))
数据存在,就更新,不存在,就插入如何处理?
有张表,里面的记录不能存在重复记录,记录存在就更新,如果不存在就插入
先查询select,如果存在就update,不存在就insert
mysql专门有种写法,
on DUPLICATE key Update
insert into news(new_title, new_abstr, new_code, update_time, create_time)
values('你是好','我是谁',MD5(CONCAT('你是好','我是谁')))
on DUPLICATE key Update
update_time=now(), create_time=now()
根据商品分类,来获取最新日期的商品销售情况
1、获取每个商品分类的最新销售日期
2、根据上面查询的日期再次查询出最新商品销售情况
select a.* from pro_sale as a inner join
(select pro_class,max(sale_date) as sale_date from pro_sale group by pro_class) as b
on a.pro_class = b.pro_class AND a.sale_date = b.sale_date order by pro_class
商品按销量排序,并要把指定几个商品顶置最前面(排序作弊)
经常会遇到按条件排序之外还要把指定商品顶置,此时该如何处理呢?
(select * from product where id in (4,2,8) order by user_total desc)
union
(select * from product where id not in (4,2,8) order by user_total desc)
select * from product order by id in (4,2,8) desc,user_total desc
分组,如何统计组内数据名称?
获取所有分类下的所有商品名称统计
select GROUP_CONCAT(pro_name),pro_type from product group by pro_type