精華 SQL語句批量提取帝國PHP系統CMS文章首圖為縮略圖
<p>通過SQSL語句批量提取新聞內容字段newstext的第一張圖片為標題圖片,對于文章模型的內容存文本的情況下無效。</p>
執行步驟:系統 - 數據庫備份與恢復 - 執行SQL語句 中執行以下語句,注意請一定要提前備份以防萬一,因為字段存的表的不同所以區分執行語句,下面以新聞模型為例。
新聞正文字段作為主表的情況用以下語句:
update ?[!db.pre!]ecms_news ?set ?titlepic ?=concat(replace(SUBSTRING_INDEX(SUBSTRING_INDEX(newstext, ?'src='http://www.viagraonlinego.com/, ?-1),'.gif',1),'"',''),'.gif') ?where ?newstext ?like ?'%.gif%' ?and ?titlepic='';
update ?[!db.pre!]ecms_news ?set ?titlepic ?=concat(replace(SUBSTRING_INDEX(SUBSTRING_INDEX(newstext, ?'src='http://www.viagraonlinego.com/, ?-1),'.jpg',1),'"',''),'.gif') ?where ?newstext ?like ?'%.jpg%' ?and ?titlepic='';
update ?[!db.pre!]ecms_news ?set ?titlepic ?=concat(replace(SUBSTRING_INDEX(SUBSTRING_INDEX(newstext, ?'src='http://www.viagraonlinego.com/, ?-1),'.png',1),'"',''),'.png') ?where ?newstext ?like ?'%.png%' ?and ?titlepic='';
新聞正文字段作為副表的情況用以下語句:
update [!db.pre!]ecms_news_data_1 a, [!db.pre!]ecms_news b set b.titlepic=concat(replace(SUBSTRING_INDEX(SUBSTRING_INDEX(a.newstext, ?'src='http://www.viagraonlinego.com/, ?-1),'.gif',1),'"',''),'.gif') ?where ?a.newstext ?like ?'%.gif%' ?and ?b.titlepic='' and a.id=b.id;
update [!db.pre!]ecms_news_data_1 a, [!db.pre!]ecms_news b set b.titlepic=concat(replace(SUBSTRING_INDEX(SUBSTRING_INDEX(a.newstext, ?'src='http://www.viagraonlinego.com/, ?-1),'.jpg',1),'"',''),'.jpg') ?where ?a.newstext ?like ?'%.jpg%' ?and ?b.titlepic='' and a.id=b.id;
update [!db.pre!]ecms_news_data_1 a, [!db.pre!]ecms_news b set b.titlepic=concat(replace(SUBSTRING_INDEX(SUBSTRING_INDEX(a.newstext, ?'src='http://www.viagraonlinego.com/, ?-1),'.png',1),'"',''),'.png') ?where ?a.newstext ?like ?'%.png%' ?and ?b.titlepic='' and a.id=b.id;
注意的是有標題圖片了的也會被提取第一張圖片,執行無差別。