Mysql Laravel Complex query to sum item quantity - php

I am trying to build complex query in laravel for making sum for item quantity.
I have total four tables used in this query. My main table Inventories table
Inventories
My datatable is displaying by looping on inventories table.
I have another table is composite item table, which contains bunch of multiple items.
Composite Inventories
composite item and inventory table has pivot table which is relation between inventory table & composite item. relations something like many to many.
Composite has inventories
Now individual items or composite item are added to purchased order table
Purchase order
Purchase order table has one child table purchase order details which Contains item & composite items.
Purchase order details
In the purchase order details inventory_item field contains item or composite item name.
I need to get total quantity of item which has approved status in purchase order table.
For example inventory_item (item) = "stest2" (quantity="10"),
inventory_item (composite item) = "aa-buycomp" (quantity="2")
Now see setes2 item = 10 + composite item (stest2 quantity)
see stest2 item id in inventories table is "22"
Now check composite item "aa-buycomp" in composite item table it has id = "1"
Now see composite_inventorie_id = "1" & inventory_id="22" & type="purchase" in composite_has_inventories table which has quantity = "1"
I need to final sum of quantity = 10 + 1 = "11"
Please help to build laravel complex query.

Related

Insert or update a row based on data in 2 columns - 1 primary and 1 unique column

I have a table structure like this:
id (primary) bigint
attributes (unique) varchar (comma separated
numbers)
product_id bigint
price text
If attributes and product id match and therefore the row already exists then I just want to update the price, otherwise, create a new row.
I have this code which is part of a loop, it will send multiple INSERT INTO queries containing different attributes, product ids and prices, right now it can't determine if the attributes and product id match and appears to only update the price on the attributes column match:
INSERT INTO my_table (id,attributes,product_id,price) VALUES ('','61','4000','500') ON DUPLICATE KEY UPDATEprice= '500'
I have attempted to define unique on both the attributes and product id columns but when doing so it updates the price on an existing row matching the attributes only (which when the product id isn't matched means the prices gets added to the wrong row) + new rows not created.
Maybe the answer is a different structure. How can I do this?
Try
INSERT INTO my_table (attributes,product_id,price) VALUES ('61','4000','500') ON DUPLICATE KEY UPDATE price= '500'

Establish a one-to-many relationship between two tables

I'm using PHP, and SQL to create a small website application for a bake shop.
I have established 2 data tables:
First one named Inventory with columns:
itemid INT(Primary key)
itemname text,
volume float,
volume type text (kg,g,lb,L,ml...)
quantity INT.
Second one named Product with columns:
prodid INT Primary Key
prodname TEXT
recipes (don't know which data type yet)
quantity INT.
The column recipes should contain numerous items from the inventory table. My friend wants it that if she make new product, the inventory database will automatically decrease the number of quantities while increasing the quantities
FOR EXAMPLE:
If she makes 100 buns(prodname) and the recipe for that 100 buns is
(quantity- itemname)
20- flour,
20- eggs,
10- sugars
then the quantity column in the inventory table should decrease by -20,-20,-10 Where the itemname are flour, eggs, sugars respectively and at the same time the product table with prodname buns should have an increase of 100 in quantity column.

Unique Columns in SQL Views

In my database I have one table that contains a complete list of products, and another table that contains the same list of products on the x-axis, with a list of customers on the y-axis, where the value for each product can be 1 or 0 depending on whether that customer can view that product. My SQL looks like this:
SELECT products.product_code, products.product_type, products.product_category, products.product_title, products.product_description
FROM product_lists
INNER JOIN products
ON product_lists.product_code=products.product_code
WHERE product_lists.customer="1"
ORDER BY products.product_code
My problem is that I would like to create a view of this result for each customer to use as that customers product table, however when I create it I get the message "This table does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available." even though the product_code field is set as a primary key in both the products table and the product_lists table.
How can I create a join/view that uses the primary key from the table(s) it was created from? In short I would like the product_code field to become the primary key of my view.
Thanks!
I think the problem is the join. You can fix this by moving the condition to the where clause. MySQL doesn't allow subqueries in the from, but it does in the where:
SELECT p.product_code, p.product_type, p.product_category, p.product_title, p.product_description
FROM products p
WHERE EXISTS (SELECT 1
FROM product_lists pl
WHERE pl.product_code = p.product_code AND
pl.customer = 1
)
ORDER BY p.product_code;

Many-to-Many MySQl PHP

I am trying to create a link table in mySQL to manage the association of products and skus. An example, the product is a t-shirt. The sku is each t-shirt's size and color combination. I want to handle products and skus many-to-many relationships because we sell items and bulk. Bulk products may be comprised of individual skus. Ergo, one sku could be associated with many products.
So, I am pretty sure I need products and skus to have a many-to-many relationship. What I don't understand though is how to poulate the Link table. In other words, when I create a product I generate a productid in the product table. When I create the sku I have a skuid in the sku table. How to I take the productid and skuid (each primary keys in the thier own table) and add them to the link table. Each skuID and productID pair should be unique so I would like those two fields to serve as my primary key.
Anyway, when I try to create the relationship in phpmyadmin using the designer I get "Error: relation not added".
so basically you are trying to link 2 tables 1 for t-shirt product and 2 thsirt size and color, for table 1 make a product id which is primary and for table 2 create a product id field similar to table 1 which will link your two tables the only difference is that in table 2 product id is not your primary key

i want to display the quantity of products user had selected to purchased but

i had a shopping cart in which there are many products. user can purchase any number of specific item or different items
e.g.
he buys 3 items of product number 1, and
he buys 2 items of product number 2.
This is stored in database as two different records for single user with his id. Now when i want to see totall number of items he has selected.
how can i do that ?
You can sum the quantity for each row that user has:
SELECT SUM(ItemQuantity) FROM CART WHERE USERID = X
with a sql query. look
SELECT SUM(items) AS 'total' FROM basket WHERE user_id = ? GROUP BY user_id
You need to pull all the records from the database for that id and purchase date and then combine the relevant columns using some basic maths.

Categories