Answer the question
In order to leave comments, you need to log in
How to properly store leftover goods when there are n warehouses?
Hello!
Need help with proper storage of leftover goods when there are n warehouses.
Now there are 3 tables, storage of goods (≈ 300,000), storage of balances (≈ 1,000,000), warehouses (5):
CREATE TABLE `products` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`pid`)
) ENGINE=InnoDB AUTO_INCREMENT=224583 DEFAULT CHARSET=utf8;
CREATE TABLE `stocks` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`stock` int(11) DEFAULT NULL,
`product` int(11) DEFAULT NULL,
`count` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `cid` (`product`)
) ENGINE=InnoDB AUTO_INCREMENT=536813420 DEFAULT CHARSET=utf8;
CREATE TABLE `stock` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(512) CHARACTER SET cp1251 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
SELECT products.id, products.name, GROUP_CONCAT(CONCAT_WS("|", stocks.stock, stocks.count) SEPARATOR ",") AS counts FROM products LEFT JOIN stocks ON products.id = stocks.product GROUP BY products.id LIMIT 0, 10
explode
Answer the question
In order to leave comments, you need to log in
Read about their Eloquent, write down the connections and then the work will be very simple.
$products = Products::with(['stocks', 'stocks.stock'])->find($id);
$product->name;
$product->stocks; // массив со стоками из stocks
$product->stocks[0]->name; // выведен данные из таблицы stock
In your tables socks.id doesn't seem to be needed at all, waste of memory and index.
This is similar to a cache (secondary) table, then you can generally get by with one table (or two), in each product you can store a list of warehouses with leftovers.
Save on indexes and it will work faster (without joins in the database).
If you need to filter by warehouse, then you can impose an index on the array of warehouses (if mysql already supports this).
isn't it easier to use inner join?
SELECT products.id, products.name, stock.name, stocks.count
from products
inner join stocks ON stocks.product = product.id
Inner join stocks ON stocks.stock =stock.id
GROUP BY products.id
further through where you can filter as you need.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question