概述
测试环境:mysql 5.6.21
步骤
创建测试表
CREATE TABLE `product` (
`proID` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品表主键',
`price` decimal(10,2) NOT NULL COMMENT '商品价格',
`type` int(11) NOT NULL COMMENT '商品类别(0生鲜,1食品,2生活)',
`dtime` datetime NOT NULL COMMENT '创建时间',
PRIMARY KEY (`proID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='商品表';
CREATE TABLE `producttype` (
`ID` int(11) NOT NULL COMMENT '商品类别(0生鲜,1食品,2生活)',
`amount` int(11) COMMENT '每种类别商品总金额',
UNIQUE KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='商品类别资金汇总表'
插入测试数据
INSERT INTO product(price,type,dtime) VALUES(10.00,0,now()),(10.00,1,now()),(10.00,1,now()),(20.00,2,now()),(30.00,3,now());
INSERT INTO producttype(ID) VALUES(1),(2),(3);
SELECT * FROM product;
SELECT * FROM producttype;
单表更新操作
UPDATE product
SET price='20.00',type=0
WHERE proID=2;
关联更新操作
UPDATE producttype,product
SET producttype.amount=product.price
where product.TYPE = producttype.ID AND product.TYPE=1;
1.查询所有字段
INSERT INTO producttype(ID) VALUES(4);
UPDATE producttype
SET producttype.amount= (SELECT ifnull(SUM(product.price),0.00) FROM product WHERE product.TYPE = producttype.ID GROUP BY product.TYPE);
注意:大家注意看到rows matched:4,表示符合条件的记录是4条,实际更新了三条,关联语句默认不去判断里面的关联的实际行。
2.只查询需要更新的字段
UPDATE producttype
SET amount=null;
UPDATE producttype,product
SET producttype.amount=
(SELECT ifnull(SUM(product.price),0.00)
FROM product
WHERE product.TYPE = producttype.ID
GROUP BY product.TYPE)
where product.TYPE = producttype.ID;
改用下面这种方法:Rows matched:3,查询到了符合条件的行是3行,没有查询无关联的行。
总结
mysql的update的关联操作在5.6中后面不能直接接FROM语句,只能UPDATE 所有的连接表然后WHERE,如果之前有事情其它数据库产品的习惯在这里要注意了。

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而ilenosqloptionslikemongodb,redis和calablesolutionsolutionsolutionsoluntionsoluntionsolundortionsolunsonstructureddata.blobobobissimplobisslowdeperformberbutslowderformandperformancewithlararengedata;

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollationsEttingSefectery.1)usecharforfixed lengengtrings,varchar forvariable-varchar forbariaible length,andtext/blobforlargerdataa.2 seterters seterters seterters

mysqloffersechar,varchar,text,and denumforstringdata.usecharforfixed Lengttrings,varcharerforvariable長度,文本forlarger文本,andenumforenforcingDataAntegrityWithaEtofValues。

優化MySQLBLOB請求可以通過以下策略:1.減少BLOB查詢頻率,使用獨立請求或延遲加載;2.選擇合適的BLOB類型(如TINYBLOB);3.將BLOB數據分離到單獨表中;4.在應用層壓縮BLOB數據;5.對BLOB元數據建立索引。這些方法結合實際應用中的監控、緩存和數據分片,可以有效提升性能。

掌握添加MySQL用戶的方法對於數據庫管理員和開發者至關重要,因為它確保數據庫的安全性和訪問控制。 1)使用CREATEUSER命令創建新用戶,2)通過GRANT命令分配權限,3)使用FLUSHPRIVILEGES確保權限生效,4)定期審計和清理用戶賬戶以維護性能和安全。

chosecharforfixed-lengthdata,varcharforvariable-lengthdata,andtextforlargetextfield.1)chariseffity forconsistent-lengthdatalikecodes.2)varcharsuitsvariable-lengthdatalikenames,ballancingflexibilitibility andperformance.3)

在MySQL中處理字符串數據類型和索引的最佳實踐包括:1)選擇合適的字符串類型,如CHAR用於固定長度,VARCHAR用於可變長度,TEXT用於大文本;2)謹慎索引,避免過度索引,針對常用查詢創建索引;3)使用前綴索引和全文索引優化長字符串搜索;4)定期監控和優化索引,保持索引小巧高效。通過這些方法,可以在讀取和寫入性能之間取得平衡,提升數據庫效率。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

WebStorm Mac版
好用的JavaScript開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器