Gorm: How to do a select random ? type:question

Created on 17 Dec 2016  路  2Comments  路  Source: go-gorm/gorm

Hi ! Thank you for you work !

I'd like to know if it's possible to do a random select without using RAW SQL ?

For example, in MySQL I would to use " ORDER BY RAND() "
and in Postgres " ORDER BY RANDOM() ".

Thank you for your help !

Most helpful comment

Order(gorm.Expr("rand()")) or Order(gorm.Expr("random()"))

All 2 comments

Order(gorm.Expr("rand()")) or Order(gorm.Expr("random()"))

Thanks @jinzhu , It works for me!

Use rand() or random() depend on the database type . For example ,

  • use rand() or Order(gorm.Expr("rand()")) in MySQL ,
  • use random() or Order(gorm.Expr("random()")) in PostgreSQL or sqlite

More database type can find solution from this answer:
https://stackoverflow.com/questions/580639/how-to-randomly-select-rows-in-sql/1209946#1209946

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sredxny picture sredxny  路  3Comments

corvinusy picture corvinusy  路  3Comments

easonlin404 picture easonlin404  路  3Comments

hypertornado picture hypertornado  路  3Comments

izouxv picture izouxv  路  3Comments