|
|
Re: Shuffle problem [message #182973 is a reply to message #172196] |
Mon, 30 September 2013 13:48 |
Erwin Moller
Messages: 228 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 9/30/2013 9:26 AM, richard wrote:
> On my site I want to create a play list that is randomly selected when the
> page is loaded.
> I have ten tables, each with 100 rows.
[sidestepping]
That strikes me as pretty strange database design.
Why do you have 10 tables if they contain the same things?
In case each table represents a year, you might want to add a
year-column to the table, and fill that with the right value.
That way you'll have one clear table.
[end sidestepping]
> I want to extract the first 20 rows from each table.
Ask yourself: What are the FIRST 10 rows?
What "first 10" means depends on your ORDER BY clause.
In general: getting the first 10 is done in a database specific manner.
For example SQL Server:
SELECT TOP 10 FROM sometablename;
In Postgres you'll use: LIMIT and OFFSET
etc.
Look it up for your database.
> Then shuffle that and create the list.
> Somehting similar to shuffling a deck of cards.
Solution: Do 10 selects, each returning 10 rows.
Store them in an array.
OR
Create one query using UNION to glue them together.
Then shuffle it in some way, for example with the shuffle function.
Good luck,
Erwin Moller
>
> So that my playlist might look like:
>
> 1965 #4
> 1963 #13
> 1969 #18
> 1961 #10
> 1960 #3
>
> What ways can I do this?
>
--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
|
|
|
|
Re: Shuffle problem [message #182980 is a reply to message #182973] |
Mon, 30 September 2013 22:24 |
Scott Johnson
Messages: 196 Registered: January 2012
Karma: 0
|
Senior Member |
|
|
On 9/30/2013 6:48 AM, Erwin Moller wrote:
> On 9/30/2013 9:26 AM, richard wrote:
>> On my site I want to create a play list that is randomly selected when
>> the
>> page is loaded.
>> I have ten tables, each with 100 rows.
>
> [sidestepping]
> That strikes me as pretty strange database design.
> Why do you have 10 tables if they contain the same things?
> In case each table represents a year, you might want to add a
> year-column to the table, and fill that with the right value.
> That way you'll have one clear table.
> [end sidestepping]
>
Oh Erwin.
Welcome to Richards world of RDB design.
I am no expert but we have all asked the same question and the answers
will make your eyes bleed.
Scotty
|
|
|
Re: Shuffle problem [message #182999 is a reply to message #172196] |
Wed, 02 October 2013 02:02 |
Lew Pitcher
Messages: 60 Registered: April 2013
Karma: 0
|
Member |
|
|
On Monday 30 September 2013 03:26, in comp.lang.php, "richard"
<noreply(at)example(dot)com> wrote:
> On my site I want to create a play list that is randomly selected when the
> page is loaded.
> I have ten tables, each with 100 rows.
> I want to extract the first 20 rows from each table.
> Then shuffle that and create the list.
> Somehting similar to shuffling a deck of cards.
>
> So that my playlist might look like:
>
> 1965 #4
> 1963 #13
> 1969 #18
> 1961 #10
> 1960 #3
>
> What ways can I do this?
$numbers = range(1, 200);
shuffle($numbers);
foreach ($numbers as $number)
{
$seq = $number % 20;
$year = 1950 + floor($number / 20);
printf("%4u #%d<br/>",$year,$seq);
}
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
|
|
|