2014-03-06 100 views
0

如何創建一個臨時表和一列100000行,並用473824hdhdas等唯一隨機值填充每行?用100000個隨機值創建一個臨時表?

謝謝!

+2

通過編寫一些代碼開始。 mysql可以用rand()生成隨機數,但是在那裏獲取非數值並不值得在mysql中執行它的麻煩。寫一些客戶端代碼(例如PHP)爲你做。 –

+0

'insert into foo(bar)select from 100000rows;'然而,你需要一個存儲函數,它返回你想要的隨機類型......是固定長度還是可變長度?我們不知道參數。我不認爲你只需要一串字母/數字就需要外部代碼。 – 2014-03-06 15:29:06

回答

3

您可以使用此:

SELECT REPLACE(UUID(), '-', '') 
FROM 
    (SELECT 
    (two_1.id + two_2.id + two_4.id + 
    two_8.id + two_16.id) AS id 
    FROM 
    (SELECT 0 AS id UNION ALL SELECT 1 AS id) AS two_1 
    CROSS JOIN (SELECT 0 id UNION ALL SELECT 2 id) AS two_2 
    CROSS JOIN (SELECT 0 id UNION ALL SELECT 4 id) AS two_4 
    CROSS JOIN (SELECT 0 id UNION ALL SELECT 8 id) AS two_8 
    CROSS JOIN (SELECT 0 id UNION ALL SELECT 16 id) AS two_16 
    ) AS init 
-- set your limit to needed count of rows 
LIMIT 100000 

-IT will generate字母獨特的行集。要創建表格,只需使用corresponding syntax

更新。由於大約有十六進制值產生的一些疑問,只需修改:

SELECT 
    CONCAT(
    CONV(SUBSTR(uuid, 1, 8), 16, 36), 
    CONV(SUBSTR(uuid, 9, 8), 16, 36), 
    CONV(SUBSTR(uuid, 17, 8), 16, 36), 
    CONV(SUBSTR(uuid, 25, 8), 16, 36) 
    ) as rnd 
FROM 
    (SELECT 
    REPLACE(UUID(), '-', '') AS uuid, 
    (two_1.id + two_2.id + two_4.id + 
    two_8.id + two_16.id) AS id 
    FROM 
    (SELECT 0 AS id UNION ALL SELECT 1 AS id) AS two_1 
    CROSS JOIN (SELECT 0 id UNION ALL SELECT 2 id) AS two_2 
    CROSS JOIN (SELECT 0 id UNION ALL SELECT 4 id) AS two_4 
    CROSS JOIN (SELECT 0 id UNION ALL SELECT 8 id) AS two_8 
    CROSS JOIN (SELECT 0 id UNION ALL SELECT 16 id) AS two_16 
    ) AS init 
LIMIT 100000 

- 現在all 36 letters0-9A-Z

+0

不確定這是否真的符合隨機性?看起來更像是「ONE」隨機值,然後是「x」排列/變化。 – DrCopyPaste

+0

這很漂亮。我想asker會想要一些能夠在十六進制之外生成字母的東西。 – 2014-03-06 15:35:29

+0

@DrCopyPaste好的完全連接會有40億個值,所以得到一個隨機樣本會有點隨機。 – 2014-03-06 15:37:08