增加字段:
alter table 表X add province_id int(11) COMMENT '省份id' AFTER province DEFAULT 1;
删除字段:
ALTER TABLE 表X DROP COLUMN 字段X
修改字段類型:
ALTER TABLE 表X ALTER COLUMN 字段X 新類型X
删除資料:
delete from 表X where borough_id=832
更新資料:
1、更新内容從同表另一字段擷取:
update 表X set keyword=title where keyword is null or keyword='';
2、更新内容從關聯表某字段擷取:
update 表X p set member_id=
(select a.member_id from 表Y a WHERE p.member_id = a.id) where member_id < 9352
3、更新一字段,取同表另外兩字段之和
update 表X set dialogue_id=(consignee+sender)
4、将字段置為空
update 表X set real_total = NUll where sell_type = 'cash_on_delivery'
5、替換某字段資料中指定的部分字元串
update 表X set content=
replace(content,'src="/images','src="http://xxx.xxx.xx/images')
where content like '%src="/images%'
6、從一字段中截取部分字元串更新到另一字段:
update 表X set picture=SUBSTRING_INDEX(big_picture, ';', 1) ;
函數注釋:截取第一個“;”左側的内容
7、如果資料後面有多餘的逗号,則清除掉
UPDATE shop_product SET keyword =
LEFT(keyword,char_length( keyword) - 1) WHERE RIGHT( keyword, 1 ) = ','
函數注釋:
char_length:傳回字元串所占的字元數,不管漢字還是數字或者是字母都算是一個字元
LEFT(str,n):傳回字元串str的最左面n個字元。
RIGHT(str,n):傳回字元串str的最右面n個字元。
8.資料庫之間的關聯查詢和更新
#shop表和另一資料庫關聯查詢,查詢shop表id為1的記錄;
select * from shop,xxx.society_join sj where shop.join_sq_id=sj.id and shop.id=1;
#更新shop表的省份,資料來自另一資料庫
update shop set shop.province=(select sj.province from xxx.society_join sj where shop.join_sq_id=sj.id ) where shop.id=1
9.擷取字段中的部分内容并更新到另一個字段;{使用函數SUBSTRING_INDEX();也可以配合函 數SUBSTRING()實作更複雜的比對}
X、函數測試:SELECT SUBSTRING_INDEX('www;mysqlcom', ';', 1); #擷取;前面的字段串
A、查詢測試:SELECT client_index_picture, SUBSTRING_INDEX(client_big_picture, ';', 1),client_big_picture from product where product.client_big_picture like '%;%' limit 10 ;
B、批量更新:update product set client_index_picture=SUBSTRING_INDEX(client_big_picture, ';', 1) ;
分組統計後,按統計結果排序
select s.* from( select orders.shop_id,orders.shop_name,shop.shop_owner_email, count(*) as num from orders left join shop on shop.shop_id=orders.shop_id where orders.pay_time is not null group by orders.shop_id ) s order by num desc limit 100
本文轉自 藝晨光 51CTO部落格,原文連結:http://blog.51cto.com/ycgit/1677030,如需轉載請自行聯系原作者