Hi, I think I saw this question being asked before, but I can't remember if their was a simple answer.

If I have a table of two columns (PK and FK) with a one to many relationship, i.e.

PK      FK
1       a
2       a
3       a
4       b
5       b
6       b
7       c
8       c
9       d


Can I easily select a truly random value of PK for each distinct value of FK?

I use the words 'truly random' as I think the term 'pseudo random' applies to the following syntax...

  SELECT PK,FK FROM above_table GROUP BY FK;


Of course I can do...

  SELECT MIN(PK),FK FROM above_table GROUP BY FK;

  SELECT MAX(PK),FK FROM above_table GROUP BY FK;


But what I would really love to do is something like...

  SELECT RANDOM(PK),FK FROM above_table GROUP BY FK;


Any general (and simple) SQL solution to this problem?

Any hacker out there who can put together a UDF in 10 minutes flat?

The problem is slightly more complex than stated, because PK is actually a multi-part primary key. In my fevered imagination I picture syntax that looks like this...

  SELECT RANDOM(ROW(PK_P1,PK_P2)),FK FROM above_table GROUP BY FK;


but that could be a garbage suggestion.

Any pointers to previous answers to similar questions would be great.

All the best,
Dan.



--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to