Tables as Three-Dimensional Arrays

Tables as Three-Dimensional Arrays

am 30.01.2006 19:42:37 von Max Cantor

This feels like a "newbie" question to ask, but I've done some research
and I just haven't quite figured it out yet.

Tables are basically two-dimensional arrays. However, I need to have
one field in each row point to an array with an arbitrary amount of
columns (it's a table containing the per-item cost of the product as a
function of quantity). If I had a discrete amount of columns, I could
just have the "pointer field" contain the ID of the corresponding row,
but this is situation seems stickier.

If I were implementing this structure in PHP, it would look something
like this:

$products[0]['prod_name'] = "Billboard Magnet";
$products[0]['prod_number'] = 100908;
$products[0]['quantity_costs'] = array(
array( 500, 1000, 2500, 5000, 10000 ),
array( .80, .66, .56, .50, .45 )
);
?>

Does MySQL have an existing infrastructure for dealing with
three-dimensional arrays, or tables with arbitrary amounts of fields?
I think I read something somewhere about "linked tables" but I haven't
been able to find any specifics.

Thanks!

Re: Tables as Three-Dimensional Arrays

am 31.01.2006 07:23:24 von Ken Chau

Well, that sounds like a classic case of normalized tables with a
foreign key pointing to a different table:

create table products (
id int primary key auto_increment,
product_number int
);

create table quantity_costs (
id int primary key auto_increment,
product_id int,
cost float,
quantity int
);

a simple query as below will get you your costs:

select * from products p, quantity_costs c where p.id = c.product_id;

------------

Ken Chau
http://www.gizzar.com