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