I have 3 tables. "machines", "products", "machine_products".
The idea in my mind is to store machine information in "machines" table and set "machine_id" column as PRIMARY & UNIQUE and do same with "products" table the column name here is "product_id".
One machine may have one or more product so they both match in "machine_products" table
Table samples are below.
machines table
+------------+------------------+
| machine_id | machine_code |
+------------+------------------+
| 1 | C01.C03.C23 M.1 |
+------------+------------------+
| 2 | C07.08.09.10 M.1 |
+------------+------------------+
| 3 | C11.12 MONT.1 |
+------------+------------------+
| 4 | C13.14.21 MONT.1 |
+------------+------------------+
| 5 | C22 MONT.1 |
+------------+------------------+
products table
+------------+--------------+
| product_id | product_code |
+------------+--------------+
| 1 | C01.00 |
+------------+--------------+
| 2 | C01.11 |
+------------+--------------+
| 3 | C01.21 |
+------------+--------------+
| 4 | C03.00 |
+------------+--------------+
| 5 | C03.01 |
+------------+--------------+
machine_products table
+----+------------+------------+
| id | machine_id | product_id |
+----+------------+------------+
| 1 | 1 | 70 |
+----+------------+------------+
| 2 | 1 | 73 |
+----+------------+------------+
| 3 | 1 | 78 |
+----+------------+------------+
| 4 | 1 | 83 |
+----+------------+------------+
| 5 | 2 | 100 |
+----+------------+------------+
| 6 | 2 | 208 |
+----+------------+------------+
| 7 | 3 | 101 |
+----+------------+------------+
| 8 | 3 | 108 |
+----+------------+------------+
| 9 | 3 | 112 |
+----+------------+------------+
| 10 | 4 | 113 |
+----+------------+------------+
My problem is I want to insert data to "machine_products" table by using "machine_code" column data
How do I add records to the machine_products table when what I have are the product_code and machine_code?
Can you try this
Insert into machine_products ( machine_id, product_id) values( (select machine_id from machines where machine_code = $machine_code), (select product_id from products where product_code=$product_code))
You can the substitute $machine_code and $product_code for your own values
You could use a cartesian product between machine and product
and insert select
insert into machine_products ( machine_id, product_id)
select a.id, b.id
from machine a
cross join products b
this way all the possibile combination betwen machine and product are inserted in machine_products table
1) Insert the data in Machines table Machines table image You get the machine table insert id ($machine_id)
2) Insert in the Products table product table image and get the product_id ($product_id) Note : You should update the $machine id in product table
3) Insert the machine_products table machine_products table image Note : Here you should update the $machine id & $product id
Related
i have a series of tables for an asset checkout system. there are 2 types of assets.. Stocks and Assets. they all have barcodess stored in a separate table. this is how my tables look
Checkouts table: (sipe_check_out)
+----+--------+----------+----------+-----+---------------------+----------+------+----------+
| id | job_id | stock_id | asset_id | qty | out_time | user_out | note | depot_id |
+----+--------+----------+----------+-----+---------------------+----------+------+----------+
| 1 | 1625 | 0 | 1026 | 1 | 0000-00-00 00:00:00 | 1288 | | 1 |
+----+--------+----------+----------+-----+---------------------+----------+------+----------+
| 2 | 1625 | 8 | 0 | 10 | 0000-00-00 00:00:00 | 1288 | | 0 |
+----+--------+----------+----------+-----+---------------------+----------+------+----------+
barcodes table: (sipe_barcodes)
+----+----------+----------+---------+
| id | stock_id | asset_id | barcode |
+----+----------+----------+---------+
| 1 | 0 | 1026 | AR2221 |
+----+----------+----------+---------+
| 2 | 8 | 0 | MAR0001 |
+----+----------+----------+---------+
Stock Table: (sipe_stock)
+-----+------------+-----------+--------+----------+---------------------+------+--------------+------+-------------+
| id | title | alt_title | status | category | last_updated | memo | replace_cost | flag | description |
+-----+------------+-----------+--------+----------+---------------------+------+--------------+------+-------------+
| 8 | Cable XLR | Cable XLR | 2 | 10 | 2019-01-03 20:45:40 | | 750 | 0 | |
+-----+------------+-----------+--------+----------+---------------------+------+--------------+------+-------------+
| 237 | Fresnel 1K | | 0 | 43 | 2019-01-02 19:08:45 | | 12000 | 0 | |
+-----+------------+-----------+--------+----------+---------------------+------+--------------+------+-------------+
Asset Table: (sipe_stock_asset)
+------+----------+-----+--------+--------+---------------------+------+---------------+-------------+-------+
| id | stock_id | qty | serial | status | status_change | memo | purchase_date | retire_date | depot |
+------+----------+-----+--------+--------+---------------------+------+---------------+-------------+-------+
| 1026 | 237 | 1 | | 0 | 2019-01-02 19:09:00 | NULL | NULL | NULL | 1 |
+------+----------+-----+--------+--------+---------------------+------+---------------+-------------+-------+
i need a list of all items checked out to a specific job_id. these items are sotred in the chekout table. but for display purpose i need to get the items barcode (stored in the barcodes table) , items title (stored in the stock table), and qty (stored in the checkout table)
this is my problem. the select query is different for a stock item and for an asset item.
if it is a stock item one needs to get the stock id from the checkout table then get the title from the stock table and the qty from the checkout table.
this is my working query to get stock items
SELECT
sipe_barcodes.barcode,
sipe_stock.title,
sipe_check_out.qty,
sipe_check_out.out_time
FROM
sipe_barcodes
INNER JOIN sipe_stock ON (sipe_barcodes.stock_id = sipe_stock.id)
INNER JOIN sipe_check_out ON (sipe_stock.id = sipe_check_out.stock_id)
WHERE
sipe_check_out.job_id = 1625
this returns the following
+---------+-----------+-----+---------------------+
| barcode | title | qty | out_time |
+---------+-----------+-----+---------------------+
| MAR0001 | Cable XLR | 10 | 0000-00-00 00:00:00 |
+---------+-----------+-----+---------------------+
If the item is a a stock. we get the asset_id from the checkout. but then we need to get the stock_id from the checkout table so we can get the stock title from the stock table. the qty from the checkout table and the barcode from the barcode table
this is my working query to get asset items
SELECT
sipe_barcodes.barcode,
sipe_stock.title,
sipe_check_out.qty,
sipe_check_out.out_time
FROM sipe_check_out
INNER JOIN sipe_stock_asset ON (sipe_check_out.asset_id = sipe_stock_asset.id)
INNER JOIN sipe_stock ON (sipe_stock_asset.stock_id = sipe_stock.id)
LEFT JOIN sipe_barcodes ON (sipe_check_out.asset_id = sipe_barcodes.asset_id)
WHERE sipe_check_out.job_id = 1625
this retuns this
+---------+------------+-----+---------------------+
| barcode | title | qty | out_time |
+---------+------------+-----+---------------------+
| AR2221 | Fresnel 1K | 1 | 0000-00-00 00:00:00 |
+---------+------------+-----+---------------------+
The Problem
how do i structure a single mysql query that can select all items checked out no matter if they are assets or stocks producing the following output
+---------+------------+-----+---------------------+
| barcode | title | qty | out_time |
+---------+------------+-----+---------------------+
| AR2221 | Fresnel 1K | 1 | 0000-00-00 00:00:00 |
+---------+------------+-----+---------------------+
| MAR0001 | Cable XLR | 10 | 0000-00-00 00:00:00 |
+---------+------------+-----+---------------------+
your help is much Appreciated
Thanks
You may use UNION with your provided select statements
SELECT
sipe_barcodes.barcode,
sipe_stock.title,
sipe_check_out.qty,
sipe_check_out.out_time
FROM
sipe_barcodes
INNER JOIN
sipe_stock ON (sipe_barcodes.stock_id = sipe_stock.id)
INNER JOIN
sipe_check_out ON (sipe_stock.id = sipe_check_out.stock_id)
WHERE
sipe_check_out.job_id = 1625
UNION ALL
SELECT
sipe_barcodes.barcode,
sipe_stock.title,
sipe_check_out.qty,
sipe_check_out.out_time
FROM
sipe_check_out
INNER JOIN
sipe_stock_asset ON (sipe_check_out.asset_id = sipe_stock_asset.id)
INNER JOIN
sipe_stock ON (sipe_stock_asset.stock_id = sipe_stock.id)
LEFT JOIN
sipe_barcodes ON (sipe_check_out.asset_id = sipe_barcodes.asset_id)
WHERE
sipe_check_out.job_id = 1625
I have three tables in a database and want to sum and compare in one SELECT statement.
The logic is that if one article runs low in stock it should be selected. An article runs low if the summarized quantity of all rows with the same artID in the table Articles in stock (an article can have several rows in the table and therefore needs to be summarized) is lower or equal to the column warning in the row with the corresponding ID. This is also warehouse specific, meaning that an article can be fully stocked in one warehouse, and at the same time running low in another. Which is why it is needed to summarize grouped on a specific warehouse.
Information about which warehouse is running low, and such as artNr for the article is also needed, which can be found in table Articles
Articles
+----+-------+---------+
| ID | artNr | warning |
+----+-------+---------+
| 1 | LA08 | 5 |
| 2 | LA09 | 10 |
| 3 | LA58 | 0 |
+----+-------+---------+
warehouse
+----+-------+
| ID | artID |
+----+-------+
| 3 | 1 |
| 4 | 1 |
| 5 | 2 |
+----+-------+
Articles in stock
+----+-------+-------------+----------+
| ID | artID | warehouseID | quantity |
+----+-------+-------------+----------+
| 1 | 1 | 3 | 10 |
| 2 | 1 | 5 | 15 |
| 3 | 2 | 5 | 45 |
| 4 | 1 | 3 | 20 |
| 5 | 3 | 5 | 4 |
+----+-------+-------------+----------+
select a.id, s.warehouseID, sum(s.quantity)
from articles a
join articles_in_stock s on a.id = s.artID
group by a.id, s.warehouseID
having sum(s.quantity) <= a.warning
I have two mysql tables named 'categories' and 'products'.
These are data of that tables.
mysql> select * from categories;
+-------------+--------+----------------------+-------------+
| category_id | parent | name | description |
+-------------+--------+----------------------+-------------+
| 1 | NULL | Products | NULL |
| 2 | 1 | Computers | NULL |
| 3 | 2 | Laptops | NULL |
| 4 | 2 | Desktop Computers | NULL |
| 5 | 2 | Tab PCs | NULL |
| 6 | 2 | CRT Monitors | NULL |
| 7 | 2 | LCD Monitors | NULL |
| 8 | 2 | LED Monitors | NULL |
| 9 | 1 | Mobile Phones | NULL |
| 10 | 9 | LG Phone | NULL |
| 11 | 9 | Anroid Phone | NULL |
| 12 | 9 | Windows Mobile | NULL |
| 13 | 9 | iPad | NULL |
| 14 | 9 | Samsung Galaxy | NULL |
| 15 | 1 | Digital Cameras | NULL |
| 16 | 1 | Printers and Toners | NULL |
| 22 | 1 | Computer Accessaries | NULL |
| 23 | 22 | USB Cables | NULL |
| 24 | 22 | Network Cables | NULL |
+-------------+--------+----------------------+-------------+
24 rows in set (0.00 sec)
mysql> select product_id, category_id from products;
+------------+-------------+
| product_id | category_id |
+------------+-------------+
| 1 | 24 |
| 2 | 6 |
| 3 | 6 |
| 4 | 6 |
+------------+-------------+
4 rows in set (0.05 sec)
Now I need to create a select query to get products to each category. I have already have category_id.
This is how I tried it:
SELECT * FROM products
WHERE category_id = 6
ORDER BY added_date DESC
My question is when I creating this select query I need all the products if one category have its subcategories. That mean, if category_id is 2, then I need to get all the products including its sub categories.
Can anybody tell me how I create this select query?
Thank you.
SELECT *
FROM products
WHERE category_id = 2 OR
category_id IN (SELECT category_id
FROM categories
WHERE parent = 2)
ORDER BY added_date DESC
try this.
select a.*,b.* from categories a inner join products b on a.category_id = b.category_id where a.category_id='$Category_id' or a.parent_id='$category_id'
You can do it using a sub-query to get category_id values whose parent is 2:
SELECT *
FROM products
WHERE category_id = 2 OR
category_id IN (SELECT category_id
FROM categories
WHERE parent = 2)
ORDER BY added_date DESC
All of the above answers will work but only for one level deep of categories. If you need sub-sub-categories then I'd suggest using something called MPTT. This will require 2 new fields in your categories database table though (typically called left and right).
I'm trying to create a query that would basically be the equivelant of this (does not work).
SELECT * FROM `categories` AS C AND
SELECT * FROM `items` AS I AND
SELECT COUNT(I.id) AS items AND
SELECT SUM(I.price) AS price;
I am not using SQLServer, and I'm using PDO through PHP for database connectivity.
Here's the tables.
Category
+----+------------+
| id | Category |
+----+------------+
| 1 | First_Cat |
| 2 | Second_Cat |
+----+------------+
items
+----+----------+------+-------+
| id | category | name | price |
+----+----------+------+-------+
| 1 | 1 | Foo | 1.99 |
| 2 | 1 | Bar | 2.00 |
| 3 | 2 | ooF | 0.99 |
| 4 | 2 | raB | 1.99 |
+----+----------+------+-------+
Based on these tables I would be expecting these query results:
+----+------------+-------+-------+--+
| id | category | items | price | |
+----+------------+-------+-------+--+
| 1 | First_Cat | 2 | 3.99 | |
| 2 | Second_Cat | 2 | 2.98 | |
+----+------------+-------+-------+--+
Any help?
The query that you have posted in the comment you are not joining the category table and the item table. That makes me think that you could do something like this?:
SELECT
categories.id,
categories.Category,
COUNT(*) AS items,
SUM(items.price) as price
FROM
`categories`
JOIN items
ON categories.id = items.category
GROUP BY
categories.id,
categories.Category
I have a table connected to another table through foreign key;
First Table is the brand; it has a brand_id and brand_name fields
Second Table is the Product Line; it has a Line_id and line_name fields
Third Table is the Lines Offered; it has a id, Brand (which is a foreign key from the first table), and Line_name (which is a foreign key from the second table.
If i will look into the third table on mysql the fields contains the id numbers of the foreign keys.
My question is this, is it possible that the stored value will be the name itself and not the ID? or Is it possible to add a field on my third table named it as Brand_name which is a VARCHAR that will display the exact brand name from the first table. Example the values of my third table would be '1','3','The Brand Name of brand with id no. 3','25'; - If yes i dont have any idea how to do this.
Nothing is preventing you from doing this. You would simply add a brand_name field to the "third table"... The question begging to be asked is: why do you want to do this?
brands
_________________________
| brand_id | brand_name |
| 1 | brand1 |
| 2 | brand2 |
| 3 | brand3 |
| 4 | brand4 |
| 5 | brand5 |
-------------------------
lines
_________________________
| line_id | line_name |
| 1 | line1 |
| 2 | line2 |
| 3 | line3 |
| 4 | line4 |
| 5 | line5 |
-------------------------
linked
_________________________________________________
| id | line_id | brand_id | brand_name |
| 1 | 5 | 1 | brand1 |
| 2 | 5 | 2 | brand2 |
| 3 | 4 | 2 | brand2 |
| 4 | 4 | 3 | brand3 |
| 5 | 4 | 3 | brand3 |
| 6 | 3 | 4 | brand4 |
| 7 | 3 | 4 | brand4 |
| 8 | 2 | 4 | brand4 |
| 9 | 2 | 5 | brand5 |
| 10 | 1 | 5 | brand5 |
------------------------------------------------
That's your proposed setup. Now if we:
SELECT brand_id, brand_name FROM linked WHERE line_id = 4;
We would get:
_________________________
| brand_id | brand_name |
| 2 | brand2 |
| 3 | brand3 |
| 3 | brand3 |
-------------------------
But the same could be achieved without duplicate data (in large databases it's pretty unreasonable to have duplicate data like that), and without needing to update BOTH the linked and brand tables every time the brand name changes by using:
SELECT linked.brand_id, brands.brand_name
FROM brands, linked
WHERE linked.line_id = 4 AND brands.brand_id = linked.brand_id;
_________________________
| brand_id | brand_name |
| 2 | brand2 |
| 3 | brand3 |
| 3 | brand3 |
-------------------------
That answer didn't need to be that long. I was having fun making tables.