|
Posted by Mikhail Kovalev on 12/05/07 19:10
On 5 Des, 18:42, Norman Peelman <npeel...@cfl.rr.com> wrote:
> Toby A Inkster wrote:
> > Mikhail Kovalev wrote:
>
> >> $array[112][COUNT] = 10;
> >> $array[112][NEXT][225][COUNT] = 3;
> >> $array[112][NEXT][225][NEXT][930][COUNT] = 1;
> >> $array[112][NEXT][225][NEXT][930][NEXT] = array();
> >> $array[112][NEXT][11][COUNT] = 5;
> >> $array[112][NEXT][11][NEXT] = array();
>
> >> An address is an array, f ex array(112, 225, 930)
>
> > If $array[112][NEXT][11] exists, does that imply that
> > $array[113][NEXT][11] cannot exist?
>
> > i.e. Can 11 have only one "parent" or multiple?
>
> > If it can only have one parent, then I'd suggest using the structure
> > suggested by "C". If '11' can have more than one parent, then a classic
> > tree structure is out of the window, so I'd suggest a simple string key,
> > like this:
>
> > CREATE TABLE nodes
> > (
> > node_address varchar(1024) NOT NULL PRIMARY KEY,
> > node_count integer
> > );
>
> > To add counts to the database, you'd use:
>
> > INSERT INTO nodes VALUES ('112', 10);
> > INSERT INTO nodes VALUES ('112/225', 3);
> > INSERT INTO nodes VALUES ('112/225/930', 1);
> > INSERT INTO nodes VALUES ('112/11', 5);
>
> > To retrieve the count for key array(112, 225, 930), you'd use:
>
> > SELECT node_count
> > FROM nodes
> > WHERE node_address='112/225/930';
>
> > To find a list of descendant nodes of '112', you'd use:
>
> > SELECT node_address
> > FROM nodes
> > WHERE node_address LIKE '112/%';
>
> > Or to find just child nodes (i.e. no grandchildren, etc):
>
> > SELECT node_address
> > FROM nodes
> > WHERE node_address LIKE '112/%'
> > AND NOT node_address LIKE '112/%/%';
>
> > By the way -- do yourself a favour and stop naming your functions things
> > like function_9() and function_10(), and variables like $array_3. If you
> > have to come back to this code after 2 or 3 months away from it, you will
> > have forgotten what they all do. Give them memorable names.
>
> INSERT INTO nodes (node_address, node_count) VALUES ($node_address,
> $node_count) ON DUPLICATE KEY UPDATE node_count=node_count+1
>
> ...will INSERT new entries and UPDATE existing entries in one swoop.
>
> Norm
Ok, suppose I'm joining to structures which have been created
separately,
from before i have ('112/225/930', 3)
and i want to add ('112/225/930', 2), which also happens to be present
in the second table, only with a different count,
to make ('112/225/930', 5)
From there I want to make it the general case so that when updating an
entry with count 1 I am actually adding ('112/225/930', 1) to the
existing one, if it exists:
INSERT INTO nodes (node_address, node_count) VALUES (<new_address>,
<new_count>) ON DUPLICATE KEY UPDATE node_count = node_count +
<new_count>
Is this correct? (Do I have to use <> in VALUES, I'm following an
example which does it?)
Navigation:
[Reply to this message]
|