Re: Database suggestion
- From: Norman Peelman <npeelman@xxxxxxxxxx>
- Date: Wed, 05 Dec 2007 14:52:58 -0500
Mikhail Kovalev wrote:
On 5 Des, 18:42, Norman Peelman <npeel...@xxxxxxxxxx> wrote:Toby A Inkster wrote:Mikhail Kovalev wrote:INSERT INTO nodes (node_address, node_count) VALUES ($node_address,$array[112][COUNT] = 10;If $array[112][NEXT][11] exists, does that imply that
$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)
$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.
$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?)
Ooops, in my other reply that should have read:
$your_variable = 2
"...ON DUPLICATE KEY UPDATE new_count = new_count + $your_variable"
....to add an arbitrary amount to new_count. 3 + 2 = 5 for your example.
Norm
.
- References:
- Database suggestion
- From: Mikhail Kovalev
- Re: Database suggestion
- From: C. (http://symcbean.blogspot.com/)
- Re: Database suggestion
- From: Mikhail Kovalev
- Re: Database suggestion
- From: Toby A Inkster
- Re: Database suggestion
- From: Norman Peelman
- Re: Database suggestion
- From: Mikhail Kovalev
- Database suggestion
- Prev by Date: Re: Database suggestion
- Next by Date: Re: Force apache to parse html files as php
- Previous by thread: Re: Database suggestion
- Next by thread: Re: Database suggestion
- Index(es):
Relevant Pages
|