Executing Different mysql selects based on a field - php

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

Related

how to show total values of different columns while using LEFT JOIN and GROUP BY

I'm working on a mlm website. It has a page called my payouts in which there are two types of payouts table i.e. silver_payout, golden_payout and the third is Total Payout which is the addition of both. I want to display the payouts datewise. Both the tables contains date column.
Silver Payout table
sp_id | username | name | sp_income | admin_charge | tds | net_payout | date
| | | | | | |
1 | super | Super Admin | 750 | 75 | 38 | 637 | 2019-10-03
| | | | | | |
2 | super | Super Admin | 750 | 75 | 38 | 637 | 2019-10-03
Golden Payout table
gp_id | username | name | gp_income | admin_charge | tds | net_payout | date
| | | | | | |
1 | super | Super Admin | 750 | 75 | 38 | 637 | 2019-10-03
| | | | | | |
2 | super | Super Admin | 750 | 75 | 38 | 637 | 2019-10-03
This is what I'm expecting as total payout
id | username | silver pay | golden pay| total pay | admin_charge| tds | net_payout | date
| | | | | | | |
1 | super | 1500 | 1500 | 3000 | 300 | 150 | 2550 | 2019-10-03
And this is what I'm getting as total payout
id | username | silver pay | golden pay| total pay | admin_charge| tds | net_payout | date
| | | | | | | |
1 | super | 3000 | 3000 | 6000 | 600 | 304 | 5096 | 2019-10-03
SELECT silver_payout.username,
COUNT(silver_payout.sp_id) AS id,
SUM(silver_payout.sp_income) AS income,
SUM(silver_payout.admin_charge) AS ach,
SUM(silver_payout.tds) AS theTDS,
SUM(silver_payout.net_payout) AS np,
silver_payout.date AS sdate,
golden_payout.username,
COUNT(golden_payout.gp_id) AS gid,
SUM(golden_payout.gp_income) AS gold_income,
SUM(golden_payout.admin_charge) AS gach,
SUM(golden_payout.tds) AS gtheTDS,
SUM(golden_payout.nett_payout) AS gnp,
golden_payout.date AS gdate
FROM (silver_payout
LEFT JOIN golden_payout
ON silver_payout.username = golden_payout.username)
WHERE silver_payout.username = '$search'
GROUP BY STR_TO_DATE(sdate,'%Y-%m-%d'), STR_TO_DATE(gdate,'%Y-%m-%d')
Any help should be appreciated.
Use union all and group by:
select username, name,
sum(income * is_silver) as s_income,
sum(income * is_gold) as g_income,
sum(income) as total_income,
. . . -- continue for the rest of the columns
from ((select s.*, 1 as is_silver, 0 as is_gold
from silver s
) union all
(select g.*, 0, 1
from gold
)
) sg
where date = ?
group by username, name

MySQL Insert into table with different column name

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

Php Two Table Update

I have a MySQL database that looks similar to this:
COUPON TABLE
| id | coupon_id | host | away |guess |skor |status|
+----+-----------+-----------+-------------+---------+------+------+
| 1 | 22 | Barcelona | Real Madrid | 1 |2-1 | 1 |
| 2 | 22 | Celtic | Porto | 2 |1-0 | 2 |
| 3 | 23 | Barcelona | Real Madrid | 1 |2-1 | 1 |
| 4 | 23 | M city | Chelsea | 1 |4-1 | 1 |
COUPONS TABLE
| id | user | Copuonstatus
+----+------------+-------------+
| 22 | Admin | 0 |
| 23 | Roberto | 0 |
I need to sort the results in a table that looks like this:
COUPONS TABLE
| id | user | Copuonstatus
+----+------------+-------------+
| 22 | Admin | 2 |
| 23 | Roberto | 1 |
NOTE :
Copuonstatus= default value 0
status = 0 waiting
status = 1 win
status = 2 los
im query try,
I could not
or update INNER JOIN use?
$q=$db->prepare("SELECT * FROM coupons INNER JOIN coupon ON
coupon.coupon_id = coupons.id AND coupon.status=2");
$q->execute();
$sss=$q->fetchAll(PDO::FETCH_GROUP);
if ( $sss > 0){
$db->exec("UPDATE coupons SET coupon_status = 2 FROM coupons ON
coupon.coupon_id = coupons.id WHERE coupons.Copuonstatus=0 ");}
UPDATE
coupons AS A
INNER JOIN coupon AS B ON A.id = B.coupon_id
SET
coupon_status = 2
WHERE
A.Copuonstatus = 0
Hope it help

MYSQL OR SQL SELECT with both COMPARE (HAVING) and GROUP

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

Basic SQL query design issue

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

Categories