一、將兩個表中符合記錄的條件查詢出來html
若是兩組符合條件的記錄有重複,且不想讓它重複的話:mysql
(SELECT name FROM A WEHERE id<100) union (SELECT name FROM B WHERE class ='分類1')sql
若是兩組符合條件的記錄有重複,且想讓它重複的話:express
(SELECT name FROM A WEHERE id<100) union all (SELECT name FROM B WHERE class ='分類1')yii2
或者:yii
$c=array_merge($a,$b);函數
二、alter & left joinui
alter tablethis
alter table `destoon_marketing_staff` add COLUMN username varchar(50) DEFAULT '' COMMENT '會員名' after userid;
alter table `destoon_push_system` add INDEX UUID(UUID);
多表查詢 url
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;
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;