2019独角兽企业重金招聘Python工程师标准>>>
1、将两个表中符合记录的条件查询出来
如果两组符合条件的记录有重复,且不想让它重复的话:
(SELECT name FROM A WEHERE id<100) union (SELECT name FROM B WHERE class ='分类1')
如果两组符合条件的记录有重复,且想让它重复的话:
(SELECT name FROM A WEHERE id<100) union all (SELECT name FROM B WHERE class ='分类1')
或者:
$c=array_merge($a,$b);
2、alter & left join
alter table
alter table `destoon_marketing_staff` add COLUMN username varchar(50) DEFAULT '' COMMENT '会员名' after userid;
alter table `destoon_push_system` add INDEX UUID(UUID);
多表查询
SELECT mobile FROM `destoon_info_775` WHERE mobile<>'' AND mobile NOT IN(SELECT userid FROM `destoon_resources`) GROUP BY `mobile`;
SELECT m.*,m2.avatar,c.address,c.telephone,c.business,c.fax from destoon_member as m,destoon_member_2 as m2 ,destoon_company as c WHERE m.userid = m2.userid and m.userid = c.userid
SELECT m.*,m2.avatar from destoon_member as m LEFT JOIN destoon_member_2 as m2 ON m.userid = m2.userid;
CREAT VIEW
CREATE VIEW destoon_sell_view AS SELECT itemid,catid,origin as areaname,title,username,company,thumb,hits,price1,price2,moq as minamount,unit,775 as moduleid,stock,status FROM destoon_info_775
UNION
SELECT itemid,catid,areaid as areaname,title,username,company,thumb,hits,price as price1,price as price2,minamount,unit,5 as moduleid,1 as stock,status FROM destoon_sell
GROUP_CONTACT
SELECT GROUP_CONCAT(catname) FROM `destoon_category` where catid IN (0,3002,2400,2403);
GROUP_CONTACT & FIND_IN_SET
select GROUP_CONCAT(catname) from `destoon_category` where find_in_set(catid,(SELECT arrchildid FROM destoon_category where catid=2429));
COUNT & GROUP BY
group by 后再对group by 的数据进行统计
select count(*) as t FROM (select count(*) FROM `destoon_member` where company != '' group by company) as total
复制一个表的部分字段更新到另外一张关联表
UPDATE destoon_member as m INNER JOIN destoon_company as c ON m.userid=c.userid SET m.business=c.business,m.telephone=c.telephone,m.address=c.address,m.vip=c.vip,m.validated=c.validates,m.fax=c.fax
复制一张表的字段添加到另外一张表
insert into destoon_info_775 (
`itemid`, `catid`, `level`, `hits`, `title`, `keyword`, `thumb`, `thumb1`, `thumb2`, `username`, `groupid`, `addtime`, `adddate`, `totime`, `company`, `vip`, `validated`, `truename`, `telephone`, `fax`, `mobile`, `address`, `email`, `qq`, `introduce`, `editor`, `edittime`, `editdate`, `ip`, `status`, `listorder`, `islink`, `linkurl`, `filepath`, `typeid`, `registration`, `model`, `brand`, `material`, `spec`, `price1`, `price2`, `unit`, `oprice`, `stock`, `moq`, `origin`, `range`, `note`, `color`, `sample`, `device`)
select
`itemid`+16000, `catid`, `level`, `hits`, `title`, `keyword`, `thumb`, `thumb1`, `thumb2`, `username`, `groupid`, `addtime`, `adddate`, `totime`, `company`, `vip`, `validated`, `truename`, `telephone`, '' as fax, `mobile`, `address`, `email`, `qq`, `introduce`, `editor`, `edittime`, `editdate`, `ip`, `status`, 999 as listorder, 0 as islink, `linkurl`, `filepath`, `typeid`, 0 as registration, `model`, `brand`, '' as material, '' as spec, `price`, `price`, `unit`, '' as oprice, 1 AS stock, `minamount`, `origin`, '' as `range`, `note`, '' as color, 1 as sample, 'pc' as device FROM destoon_sell WHERE username != '' and groupid IN (6,8) and status = 3
查询本关联的两张表没有关联上的特殊记录
SELECT * FROM `destoon_info_775` WHERE username not in(select username from destoon_member) group by username
having去掉重复记录
select trim(company),count(*) as count from destoon_member group by trim(company) having count>1;
if函数
update destoon_member set aid = if(aid = 1,0, 1) where username = 'ad2015'
统计group by 后的记录数量
SELECT COUNT(DISTINCT UUID) as count FROM destoon_phonebook
按天统计本月注册数量
select FROM_UNIXTIME(`regtime`, '%Y%m%d') as dd, count(*) as count from destoon_member WHERE regtime > UNIX_TIMESTAMP('2015-10-01') and now() group by dd
对查询出来的结果进行匹配度排序
SELECT * FROM `area_info` WHERE areaName LIKE '%县%' ORDER BY INSTR(areaName,"县")
mysql启动报错:
[ERROR] InnoDB: Attempted to open a previously opened tablespace. Previous tablespace yii2advanced/wechat_mp_user uses space ID: 9 at filepath: .\yii2advanced\wechat_mp_user.ibd. Cannot open tablespace zgzszjcom/zs_images which uses space ID: 9 at filepath: .\zgzszjcom\zs_images.ibd
解决办法:
just under [mysqld] add this line: innodb_force_recovery = 1
..
[mysqld]
innodb_force_recovery = 1
..
参考:http://stackoverflow.com/questions/18575755/xampp-mysql-could-not-open-single-table-tablespace-file-mysql-innodb-index-st
MySQL查询不区分大小写的sql写法:
第一种方法:
要让mysql查询区分大小写,可以:
select * from some_table where binary str='abc'
select * from some_table where binary str='ABC'
第二方法:
在建表时时候加以标识
create table some_table(
str char(20) binary
)
SELECT * FROM `t_chains_relation` WHERE chains_store_id = 93 and type = 1 and FIND_IN_SET(chains_id,(SELECT GROUP_CONCAT(product_id) FROM `t_product` WHERE store_id = 93 and deleted_at is null))
SELECT GROUP_CONCAT(relation_id) FROM `t_chains_relation` WHERE chains_store_id = 93 and type = 1 and FIND_IN_SET(chains_id,(SELECT GROUP_CONCAT(product_id) FROM `t_product` WHERE store_id = 93 and deleted_at is null))
行列转换并对转换后的数据进行横向和竖向统计:
SELECT
staff_id,
staff_name,
SUM(
CASE subject_id
WHEN 1 THEN
money
ELSE
0
END
) AS sale_moneys,
SUM(
CASE subject_id
WHEN 1 THEN
performance_val
ELSE
0
END
) AS sale_vals,
SUM(
CASE subject_id
WHEN 2 THEN
money
ELSE
0
END
) AS build_moneys,
SUM(
CASE subject_id
WHEN 2 THEN
performance_val
ELSE
0
END
) AS build_vals,
SUM(
CASE subject_id
WHEN 3 THEN
money
ELSE
0
END
) AS package_card_moneys,
SUM(
CASE subject_id
WHEN 3 THEN
performance_val
ELSE
0
END
) AS package_card_vals,
SUM(
CASE subject_id
WHEN 4 THEN
money
ELSE
0
END
) AS cash_card_moneys,
SUM(
CASE subject_id
WHEN 4 THEN
performance_val
ELSE
0
END
) AS cash_card_vals,
SUM(
CASE subject_id
WHEN 5 THEN
money
ELSE
0
END
) AS vip_card_moneys,
SUM(
CASE subject_id
WHEN 5 THEN
performance_val
ELSE
0
END
) AS vip_card_vals,
sum(money) AS moneys,
sum(performance_val) AS performance_vals
FROM
`t_performance_staff`
GROUP BY
staff_name
UNION
SELECT
'总计',
'总计',
SUM(sale_moneys) AS sale_moneys,
SUM(sale_vals) AS sale_vals,
SUM(build_moneys) AS build_moneys,
SUM(build_vals) AS build_vals,
SUM(package_card_moneys) AS package_card_moneys,
SUM(package_card_vals) AS package_card_vals,
SUM(cash_card_moneys) AS cash_card_moneys,
SUM(cash_card_vals) AS cash_card_vals,
SUM(vip_card_moneys) AS vip_card_moneys,
SUM(vip_card_vals) AS vip_card_vals,
SUM(moneys) AS moneys,
SUM(performance_vals) AS performance_vals
FROM
(
SELECT
staff_id,
staff_name,
SUM(
CASE subject_id
WHEN 1 THEN
money
ELSE
0
END
) AS sale_moneys,
SUM(
CASE subject_id
WHEN 1 THEN
performance_val
ELSE
0
END
) AS sale_vals,
SUM(
CASE subject_id
WHEN 2 THEN
money
ELSE
0
END
) AS build_moneys,
SUM(
CASE subject_id
WHEN 2 THEN
performance_val
ELSE
0
END
) AS build_vals,
SUM(
CASE subject_id
WHEN 3 THEN
money
ELSE
0
END
) AS package_card_moneys,
SUM(
CASE subject_id
WHEN 3 THEN
performance_val
ELSE
0
END
) AS package_card_vals,
SUM(
CASE subject_id
WHEN 4 THEN
money
ELSE
0
END
) AS cash_card_moneys,
SUM(
CASE subject_id
WHEN 4 THEN
performance_val
ELSE
0
END
) AS cash_card_vals,
SUM(
CASE subject_id
WHEN 5 THEN
money
ELSE
0
END
) AS vip_card_moneys,
SUM(
CASE subject_id
WHEN 5 THEN
performance_val
ELSE
0
END
) AS vip_card_vals,
sum(money) AS moneys,
sum(performance_val) AS performance_vals
FROM
`t_performance_staff`
GROUP BY
staff_name
) AS b
代码量太大,对查询的结果保存到视图中,并对视图进行统计
SELECT
*
FROM
`view_performance_staff`
UNION
SELECT
'合计',
'合计',
SUM(sale_moneys) AS sale_moneys,
SUM(sale_vals) AS sale_vals,
SUM(build_moneys) AS build_moneys,
SUM(build_vals) AS build_vals,
SUM(package_card_moneys) AS package_card_moneys,
SUM(package_card_vals) AS package_card_vals,
SUM(cash_card_moneys) AS cash_card_moneys,
SUM(cash_card_vals) AS cash_card_vals,
SUM(vip_card_moneys) AS vip_card_moneys,
SUM(vip_card_vals) AS vip_card_vals,
SUM(moneys) AS moneys,
SUM(performance_vals) AS performance_vals
FROM
view_performance_staff;
查看Mysql表分区语句
SELECT
partition_name part,
partition_expression expr,
partition_description descr,
table_rows
FROM
INFORMATION_SCHEMA.partitions
WHERE
TABLE_SCHEMA =
schema
()
AND
TABLE_NAME=
'table'
;
用一个表的字段填充另一个表
UPDATE t_weixin_coupon_record INNER JOIN t_store ON t_weixin_coupon_record.store_id=t_store.store_id SET t_weixin_coupon_record.consume_store=t_store.store_name;