我为一个表创建了一个slug字段,它们需要是唯一的但是其中一些不是这样我认为我可以在它们的末尾添加随机字符以使它们唯一。
所以这是我的SELECT查询:
SELECT slug,count(*) as num FROM table GROUP BY slug HAVING num > 1
这是UPDATE查询:
UPDATE table SET slug = CONCAT(slug,'-',SUBSTRING(MD5(NOW()),1,2))
我无法将这2个查询合并。
答案 0 :(得分:1)
您可以使用JOIN
:
UPDATE mytable AS m
JOIN (SELECT slug
FROM mytable
GROUP BY slug
HAVING COUNT(*) > 1) t
ON m.slug = t.slug
SET m.slug = CONCAT(m.slug,'-',SUBSTRING(MD5(NOW()),1,2))
但是上面的问题是它没有生成唯一的字符串值。
解决此问题的一种方法是使用由变量计算的行数:
UPDATE mytable AS m
CROSS JOIN (SELECT @row_number:=0, @slug := '') AS vars
JOIN (
SELECT slug
FROM mytable
GROUP BY slug
HAVING COUNT(*) > 1 ) AS t ON m.slug = t.slug
SET m.slug = CONCAT(m.slug,
'-',
IF (@slug = m.slug,
IF (@slug := m.slug,@row_number:=@row_number+1,
@row_number:=@row_number+1),
IF (@slug := m.slug, @row_number:=1,
@row_number:=1))
)
答案 1 :(得分:0)
尝试:
- !ruby/object:Google::APIClient::Schema::Youtube::V3::SearchResult
data:
kind: youtube#searchResult
etag: ! '"Y3xTLFF3RLtHXX85JBgzzgp2Enw/RtuotxAng6F5sgkBlbg_LTSDi7s"'
id:
kind: youtube#video
videoId: d5TsLsjtth8
snippet:
publishedAt: '2015-06-29T04:19:43.000Z'
channelId: UCxdQj_S7w5O9lEm6v2uH_EA
title: ! '◕‿◕☞ Funny Cats Funny Cat Videos Funny Cat Vine Most Funny Cat
2016 #2'
description: funny vines clean version funny vines compilation 2015 new funny
vines christian delgrosso funny vines cartoon voice overs funny fails dirt
bike funny fails don't ...
thumbnails:
default:
url: https://i.ytimg.com/vi/d5TsLsjtth8/default.jpg
medium:
url: https://i.ytimg.com/vi/d5TsLsjtth8/mqdefault.jpg
high:
url: https://i.ytimg.com/vi/d5TsLsjtth8/hqdefault.jpg
channelTitle: ''
liveBroadcastContent: none
- !ruby/object:Google::APIClient::Schema::Youtube::V3::SearchResult
data:
kind: youtube#searchResult
etag: ! '"Y3xTLFF3RLtHXX85JBgzzgp2Enw/wXXxhJIuGlDPCjNyD6fxNnnp9yA"'
id:
kind: youtube#video
videoId: 812W82_VDaA
snippet:
publishedAt: '2015-06-29T03:57:38.000Z'
channelId: UCH9ev60cc7IFU_fqhBEpChw
title: ! 'The Cat, the Vagabond and the Victim: A Cats in Trouble'
description: ! 'The Cat, the Vagabond and the Victim: A Cats in Trouble http://www.amazon.co.uk/dp/B00U259982?tag=weiradlaemm06-21
Feature * Heir of the cat... When ...'
thumbnails:
default:
url: https://i.ytimg.com/vi/812W82_VDaA/default.jpg
medium:
url: https://i.ytimg.com/vi/812W82_VDaA/mqdefault.jpg
high:
url: https://i.ytimg.com/vi/812W82_VDaA/hqdefault.jpg
channelTitle: ''
liveBroadcastContent: none
- !ruby/object:Google::APIClient::Schema::Youtube::V3::SearchResult
data:
kind: youtube#searchResult
etag: ! '"Y3xTLFF3RLtHXX85JBgzzgp2Enw/QWxRfjhIEHs95Rw1BSFjPwamYVY"'
id:
kind: youtube#video
videoId: 4c2OvbRT1ZQ
snippet:
publishedAt: '2015-06-29T03:51:47.000Z'
channelId: UCvrbFkZE2CXP8SVK3w5XS4A
title: ネコのケンカ? - cat fight? -
description: 諦めなければいつかは叶うw.
thumbnails:
default:
url: https://i.ytimg.com/vi/4c2OvbRT1ZQ/default.jpg
medium:
url: https://i.ytimg.com/vi/4c2OvbRT1ZQ/mqdefault.jpg
high:
url: https://i.ytimg.com/vi/4c2OvbRT1ZQ/hqdefault.jpg
channelTitle: ''
liveBroadcastContent: none
- !ruby/object:Google::APIClient::Schema::Youtube::V3::SearchResult
data:
kind: youtube#searchResult
etag: ! '"Y3xTLFF3RLtHXX85JBgzzgp2Enw/Ezf1iUMSd4dY_5GJ7Rb0MxDaJcI"'
id:
kind: youtube#video
videoId: -n040rD3nxI
snippet:
publishedAt: '2015-06-29T02:40:30.000Z'
channelId: UCsukgxUI8Gkju1lKCOKBlcQ
title: Funny Cat Joke
description: King of shitty 10 second videos.
thumbnails:
default:
url: https://i.ytimg.com/vi/-n040rD3nxI/default.jpg
medium:
url: https://i.ytimg.com/vi/-n040rD3nxI/mqdefault.jpg
high:
url: https://i.ytimg.com/vi/-n040rD3nxI/hqdefault.jpg
channelTitle: realchris1Returns
liveBroadcastContent: none
- !ruby/object:Google::APIClient::Schema::Youtube::V3::SearchResult
data:
kind: youtube#searchResult
etag: ! '"Y3xTLFF3RLtHXX85JBgzzgp2Enw/TNpAkcwloC58Xi8aCVsmSXMspyA"'
id:
kind: youtube#video
videoId: e5LJvFUM9k4
snippet:
publishedAt: '2015-06-29T02:40:12.000Z'
channelId: UC64ta8zKaK5gXWPgTk37VNA
title: ! 'Sam & Cat Season 1 : FULL Episode 1 - 36 FREE STREAMING HD QUALITY'
description: ! 'Sam & Cat - Season 1 : Episode 1 - 36 FULL STREAMING , Visit
Link On List All Episode http://youtube.vg/HVDX3KE8 And Enjoyed For 720p Until
1280p High ...'
thumbnails:
default:
url: https://i.ytimg.com/vi/e5LJvFUM9k4/default.jpg
medium:
url: https://i.ytimg.com/vi/e5LJvFUM9k4/mqdefault.jpg
high:
url: https://i.ytimg.com/vi/e5LJvFUM9k4/hqdefault.jpg
channelTitle: ''
liveBroadcastContent: none
- !ruby/object:Google::APIClient::Schema::Youtube::V3::SearchResult
data:
kind: youtube#searchResult
etag: ! '"Y3xTLFF3RLtHXX85JBgzzgp2Enw/HcqCoYiw3qvNoxloToHL_LeyAk4"'
id:
kind: youtube#video
videoId: x4RB5oF72AU
snippet:
publishedAt: '2015-06-29T02:07:49.000Z'
channelId: UCFPV7m24Y-KM66v_aScWTRQ
title: Kucing Berani serang Ular ( cat vs snake )
description: Kucing Berani serang Ular ( cat vs snake )
thumbnails:
default:
url: https://i.ytimg.com/vi/x4RB5oF72AU/default.jpg
medium:
url: https://i.ytimg.com/vi/x4RB5oF72AU/mqdefault.jpg
high:
url: https://i.ytimg.com/vi/x4RB5oF72AU/hqdefault.jpg
channelTitle: ''
liveBroadcastContent: none
- !ruby/object:Google::APIClient::Schema::Youtube::V3::SearchResult
data:
kind: youtube#searchResult
etag: ! '"Y3xTLFF3RLtHXX85JBgzzgp2Enw/dTOXDCV4PSk4woUH3RTDfYWH7sc"'
id:
kind: youtube#video
videoId: cfuq0Qs4154
snippet:
publishedAt: '2015-06-29T02:05:19.000Z'
channelId: UCH1vIUNu9z4hosPO4IiEDKg
title: MY CUTE CAT VIDEO XD
description: I MADE THIS VIDEO FOR FUN BECAUSE IT WOULD BE A CUTE FUN IDEAXD.
thumbnails:
default:
url: https://i.ytimg.com/vi/cfuq0Qs4154/default.jpg
medium:
url: https://i.ytimg.com/vi/cfuq0Qs4154/mqdefault.jpg
high:
url: https://i.ytimg.com/vi/cfuq0Qs4154/hqdefault.jpg
channelTitle: Nlg31
liveBroadcastContent: none
- !ruby/object:Google::APIClient::Schema::Youtube::V3::SearchResult
data:
kind: youtube#searchResult
etag: ! '"Y3xTLFF3RLtHXX85JBgzzgp2Enw/3VF2zQBJ71Si2Zcar34cJGKDp6A"'
id:
kind: youtube#playlist
playlistId: PLMPGTLYCAiq9gAh6UnpaXx_PtKENmh1P6
snippet:
publishedAt: '2015-06-29T01:52:34.000Z'
channelId: UCl65ZDJ_D2QnvzknOzC_Syw
title: Black Cat
description: ''
thumbnails:
default:
url: https://i.ytimg.com/vi/UexNlx4mj1g/default.jpg
medium:
url: https://i.ytimg.com/vi/UexNlx4mj1g/mqdefault.jpg
high:
url: https://i.ytimg.com/vi/UexNlx4mj1g/hqdefault.jpg
channelTitle: ''
liveBroadcastContent: none
- !ruby/object:Google::APIClient::Schema::Youtube::V3::SearchResult
data:
kind: youtube#searchResult
etag: ! '"Y3xTLFF3RLtHXX85JBgzzgp2Enw/nldPN_K-A-jNCqF0I3MlOjByOog"'
id:
kind: youtube#video
videoId: INraypSN3Q0
snippet:
publishedAt: '2015-06-29T01:35:33.000Z'
channelId: UCVbqIMbp2qRhK6-YXYkOF7g
title: ! 'Aksi kucing dubsmash lucu.. #funny #cat #pet'
description: Hilarious cats.
thumbnails:
default:
url: https://i.ytimg.com/vi/INraypSN3Q0/default.jpg
medium:
url: https://i.ytimg.com/vi/INraypSN3Q0/mqdefault.jpg
high:
url: https://i.ytimg.com/vi/INraypSN3Q0/hqdefault.jpg
channelTitle: ''
liveBroadcastContent: none
- !ruby/object:Google::APIClient::Schema::Youtube::V3::SearchResult
data:
kind: youtube#searchResult
etag: ! '"Y3xTLFF3RLtHXX85JBgzzgp2Enw/2_PMO7FctA_79aqVhV0D83Yw7VY"'
id:
kind: youtube#video
videoId: Bt6NgC0peLI
snippet:
publishedAt: '2015-06-29T01:06:00.000Z'
channelId: UCpM_hmOVus872lSLIdbirXQ
title: ! 'Conversation With a Cat: Should I Go Vegan?'
description: I'm 80% vegan, but my cat won't hesitate to eat lizards/birds...
thumbnails:
default:
url: https://i.ytimg.com/vi/Bt6NgC0peLI/default.jpg
medium:
url: https://i.ytimg.com/vi/Bt6NgC0peLI/mqdefault.jpg
high:
url: https://i.ytimg.com/vi/Bt6NgC0peLI/hqdefault.jpg
channelTitle: LifeLibertyNow
liveBroadcastContent: none
演示:http://sqlfiddle.com/#!9/01c68/1
我使用的是UPDATE t ,
( SELECT slug FROM t
GROUP BY slug HAVING count(*) > 1
) x
SET t.slug = CONCAT(t.slug,'-',SUBSTRING(MD5(rand()),1,2))
WHERE t.slug = x.slug
;
而不是rand()
,因为后者不会提供唯一的字符串。