【问题标题】:Select Record Conditional Random #MySQL选择记录条件随机#MySQL
【发布时间】:2013-10-24 20:42:22
【问题描述】:

我有两张表,一张用于静止图像,一张用于此属性,两者都链接在一起,因此属性可以没有图片。你想“选择”我可以选择所有属性的地方,只是这个属性的随机图片。如何做到这一点?

最好的例子是,我从图像表中为表中包含静止图像的每条记录选择一个随机图像。想随机选择这个。我能做的是为银行财产的每条记录选择所有图像。我只希望每条记录都包含一个各自的随机图像。

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()

【问题讨论】:

  • 那么,这是 SQL Server 还是 MySQL?
  • 很抱歉,您的问题很难理解。我认为英语不是您的母语,也许有人可以帮助您更好地翻译它?
  • 抱歉,我是印度人#TranslateGoogle

标签: mysql sql sql-server tsql


【解决方案1】:

在 SQL Server 中,在SELECT 之后添加TOP 1,并排序为ORDER BY NEWID()

在 MySQL 中,顺序应该是ORDER BY RAND(),然后是LIMIT 1

应该可以的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 2019-10-02
    • 2023-03-25
    • 2011-04-08
    • 1970-01-01
    • 2017-06-28
    相关资源
    最近更新 更多