2013-10-24 138 views
0

我有兩個表,一個用於靜止圖像,另一個用於此屬性,兩者都鏈接,因此屬性可以沒有圖片。你想「選擇」我可以在哪裏選擇所有屬性,並只是一個隨機的圖片屬性。這個怎麼做?選擇記錄條件隨機#MySQL

舉例說明,我從圖像表中選取了一幅隨機圖像,用於表格中靜止圖像的每個記錄。想隨機選擇這個。我可以做的是爲每個銀行財產記錄選擇所有圖像。我只是希望每個記錄能夠挖掘出各自的隨機圖像。

的SQL低於:

select distinct 

    imovel.nome as 'NOME', 
    imovel.valor as 'VALOR', 
    imovel.quarto as 'QUARTO', 
    imovel.suite as 'SUITE', 

    cidade.nome as 'CIDADE', 
    bairro.nome as 'BAIRRO', 
    (select distinct nome from tabimagem img where img.cdimovel = imagem.cdimovel order by rand() limit 1) as 'IMAGEM' 

from tabimovel imovel 

    inner join tabcidade cidade on imovel.cdcidade = cidade.codigo 
    inner join tabbairro bairro on imovel.cdBairro = bairro.codigo      
    inner join tabimagem imagem on imagem.cdImovel = imovel.codigo 

order by rand() 
+3

那麼,這是SQL Server還是MySQL? – Lamak

+0

很抱歉,但很難理解你的問題。我認爲英語不是您的第一語言,也許有人可以幫助您更好地翻譯它? –

+0

對不起,我是印度人#TranslateGoogle –

回答

1

在SQL Server中添加TOP 1SELECT後,和秩序ORDER BY NEWID()

在MySQL中,順序應該是ORDER BY RAND(),然後是LIMIT 1

這應該工作。