I have three table.First table shows input data, second table shows output data and third is total table where we shows our product total balance.
1.input table
Date | product_name | in_qty
5/7/19 | A | 10
5/7/19 | B | 15
6/7/19 | A | 10
6/7/19 | C | 20
2.output table
Date | product_name | out_qty
7/7/19 | A | 10
8/7/19 | B | 10
3.total balance table
product_name | in_qty | out_qty | total_qty
A | 20 | 10 | 10
B | 15 | 10 | 5
There my problem is in input table product C is not show in the total table. Which product add in the output table only those product total shown in the total table.
$res=mysqli_query($con, "SELECT i.product_name, i.in_qty, o.product_name,o.out_qty
FROM input i, output o
WHERE i.product_name= o.product_name");
while($row=mysqli_fetch_array($res))
{
$in_name = $row['product_name'];
$inqty = $row['Input_qty'];;
$out_name = $row['product_name'];
$outqty = $row['Out_qty'];
$sql=mysqli_query($con, "INSERT INTO total (product_name,Input_qty,Out_qty)
VALUES('$in_name','$inqty','$outqty')");
$sql2=mysqli_query($con, "UPDATE total t2
INNER JOIN (
SELECT product_name, SUM(in_qty) as qty_total
FROM input
GROUP BY product_name
) t1 ON t2.product_name= t1.product_name
SET t2.in_qty = t1.qty_total");
}
My expectation is :
3.total balance table
product_name | in_qty | out_qty | total_qty
A | 20 | 10 | 10
B | 15 | 10 | 5
C | 20 | | 20
your mysql query is wrong.
use following query .
SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;
in your case:
SELECT input.product_name , input.in_qty , output.out_qty
FROM input
LEFT [OUTER] JOIN output
ON input.column = output.column;
Related
I have 2 tables let's say orders
id | memberid | productsid |
----------------------------
1 | 23 | 25,27
and products
id | product_name | price |
----------------------------
25 | product1 | 120
27 | product2 | 50
I want to join orders and products table to get product name and price for each id from productsid.
This is the way how I tried to store an order for a member. If you have any better solution, I am waiting to know.
You can join the tables with the help of the function find_in_set(), then group by each order and with group_concat() create a list of the products:
select o.id, o.memberid,
group_concat(p.product_name order by find_in_set(p.id, o.productsid)) products
from orders o inner join products p
on find_in_set(p.id, o.productsid)
group by o.id, o.memberid
See the demo.
Results:
| id | memberid | products |
| --- | -------- | ----------------- |
| 1 | 23 | product1,product2 |
I have three tables :
mls_category
points_martix
mls_entry
My first table (mls_category) is like below:
*--------------------------------*
| cat_no | store_id | cat_value |
*--------------------------------*
| 10 | 101 | 1 |
| 11 | 101 | 4 |
*--------------------------------*
My second table (points_martix) is like below:
*----------------------------------------------------*
| pm_no | store_id | value_per_point | max_distance |
*----------------------------------------------------*
| 1 | 101 | 1 | 10 |
| 2 | 101 | 2 | 50 |
| 3 | 101 | 3 | 80 |
*----------------------------------------------------*
My third table (mls_entry) is like below:
*-------------------------------------------*
| user_id | category | distance | status |
*-------------------------------------------*
| 1 | 10 | 20 | approved |
| 1 | 10 | 30 | approved |
| 1 | 11 | 40 | approved |
*-------------------------------------------*
I am using the following query to show the sum of distance with some condition:
SELECT SUM(t1.totald/c.cat_value)
AS total_distance
FROM mls_category c
JOIN
(SELECT SUM(distance) totald, user_id, category
FROM mls_entry
WHERE user_id = 1
AND status = 'approved'
GROUP BY user_id, category) t1
ON c.cat_no = t1.category
This gives me sum 60 as total_distance, that is correct which I wanted.
Now, I want to include the third table (points_matrix) and want to compare my sum(60) is less than or equal to 80(max_distance) then my new value would be 60*3=180.
So, suppose my sum comes 10 then my new value will be 10*1=10 and if my sum comes 25 then my new value will be according to point matrix 25*2=50.
Yon can using MIN() to calculate what value_per_point you need, and the whole sql is like this:
SELECT MIN(b.value_per_point) * d.total_distance FROM points_matrix b
JOIN
(
SELECT store_id, sum(t1.totald/c.cat_value) as total_distance FROM mls_category c
JOIN
(
SELECT SUM(distance) totald, user_id, category FROM mls_entry
WHERE user_id= 1 AND status = 'approved' GROUP BY user_id, category
) t1 ON c.cat_no = t1.category
) d ON b.store_id = d.store_id AND b.max_distance >= d.total_distance
Use Correlated Subquery:
SELECT
dt.total_distance * dt.max_points
FROM (
SELECT SUM(t1.totald/c.cat_value) AS total_distance,
(
SELECT value_per_point
FROM points_martix
WHERE SUM(t1.totald/c.cat_value) >= max_distance
ORDER BY max_distance ASC LIMIT 1
) AS max_points
FROM mls_category AS c
JOIN (
SELECT SUM(distance) AS totald,
user_id,
category
FROM mls_entry
WHERE user_id= 1 AND
status = 'approved'
GROUP BY user_id, category
) AS t1 on c.cat_no = t1.category
) AS dt
enter image description here I am having one table like id, sale_id, item_total, tax fields. need to sum the item_total by grouping the tax values.
Table 1
id | sale_id | item_cost_price | tax |
1 | 10 | 150 | 5 |
2 | 10 | 50 | 7 |
3 | 10 | 30 | 5 |
this is required output:
id | sale_id | item_cost_price | tax |
1 | 10 | 180 | 5 |
2 | 10 | 50 | 7 |
When i tried this query,
SELECT sale_id,tax FROM bgs_ib_sales_items GROUP BY tax
$query=$this->db->query("SELECT sale_id,tax FROM bgs_ib_sales_items GROUP BY tax ");
echo $num = $query->num_rows();
$result=array();
foreach($query->result() as $row){
$result_row[]=$row->sale_id;
$result_row[]=$row->tax;
$result_row[]=$row->item_cost_price;
}
My output is:
i am getting output like this,
am getting distinct tax only. but i need to sum item total values.
Note:
Image 1 : refer my datatable
Image 2: refer my expected outputenter image description here
Add SUM(item_cost_price) in SELECT statement.
In your select statement your select only sale_id, tax. But what you echo are sale_id, tax, and item_cost_price which not exist in your SELECT statement. Try This:-
$sql = "SELECT sale_id,tax, SUM(item_cost_price ) AS TotalPrice FROM bgs_ib_sales_items WHERE sale_id = '10' GROUP BY tax";
$query=$this->db->query($sql);
foreach($query->result() as $row){
$result_row[]=$row->sale_id;
$result_row[]=$row->tax;
$result_row[]=$row->TotalPrice;
}
What is the way to sort a MYSQL result from multiple tables?
I have two tables. The first:
"store_products" table:
+----+-----------+
| id | name |
+----+-----------+
| 1 | Product 1 |
| 2 | Product 2 |
| 3 | Product 3 |
+----+-----------+
Here i placed product names. Other table contains prices for different product variants:
"store_products_variants" table:
+-----+------------+-------------+-------------+
| id | product_id | price_sale | ordering |
+-----+------------+-------------+-------------+
| 5 | 1 | 06.00 | 2 |
| 6 | 1 | 32.00 | 3 |
| 11 | 1 | 56.00 | 1 |
| 14 | 2 | 09.00 | 1 |
| 44 | 3 | 15.00 | 1 |
+-----+------------+-------------+-------------+
I need to create a sort on price (lowest and highest), that uses only first variant - ordered by column "ordering" from "store_products_variants" table.
From example above, the results should be:
+---+------------+---------------+
| 1 | Product 2 | (price 09.00) |
| 3 | Product 3 | (price 15.00) |
| 2 | Product 1 | (price 56.00) |
+---+------------+---------------+
Is this possible in MySQL?
Use the ordering column to join the correct variant onto the product.
This would be the query if the correct ordering value was always the 1.
SELECT
products.name,
variants.price_sale
FROM store_products AS products
INNER JOIN store_products_variants AS variants
ON variants.product_id = products.id
AND variants.ordering = 1
ORDER BY variants.price_sale ASC
This query will first look for the lowest ordering value of a product. Then use it to join the price on your result:
SELECT
products.name,
variants.price_sale
FROM
store_products AS products
INNER JOIN (
SELECT
product_id,
MIN(ordering) AS ordering
FROM
store_products_variants
GROUP BY
product_id
) AS variantOrdering
ON variantOrdering.product_id = products.id
INNER JOIN store_products_variants AS variants
ON variants.product_id = variantOrdering.product_id
AND variants.ordering = variantOrdering.ordering
ORDER BY
variants.price_sale ASC
select t.* from(
select t1.[id], t1.[name],
'(price ' + cast(max(t2.[price_sale]) as varchar(50)) + ')' as [price]
from [#store_products] t1
left join [#store_products_variants] t2
on t1.[id] = t2.[]product_id
group by t1.[id], t1.[name]
)t
Order by len(t.[price]), t.[price];
Yes this is possible. Try to use the JOIN command, and JOIN on the ID of each table.
Hello this is possible please use this query
SELECT p.id,p.name,spv.price_sale
from products p
INNER JOIN store_products_variants spv ON spv.product_id = p.id
where spv.ordering = 1
group by p.id
order by spv.price_sale asc
i think it gives the result what you want
I have two tables named sales and rsales
I have this ff value for table sales
id | pcode | total | discount |
2 | 33 | 100 | 20 |
3 | 33 | 100 | 20 |
and i have this ff value for table rsales
id | pcode | total | discount | sales_id |
4 | 33 | 100 | 20 | 1 |
5 | 33 | 100 | 20 | 2 |
6 | 33 | 100 | 20 | 3 |
My problem is that when I update all values from table sales, the table rsales must be update either if sales.id is equal to sales_id.
so for example if I have updated table sales with id = 2 and 3, the sales_id 2 and 3 from rsales must be updated either.
take note : only 2 and 3 because only that ids is found in table sales ids.
I have this ff codes so far to update table rsales. but the output is shown not as what I meant. it update all values.
mysql_query("UPDATE sales AS t1, rsales AS t2
SET t1.total = '$total_discount',
t2.total = '$total_discount',
t2.discount = '$tot'
WHERE t1.pcode = '$pcode'");
You should add to the WHERE class the common column between the tables.
UPDATE sales AS t1, rsales AS t2
SET
t1.total = '$total_discount',
t2.total = '$total_discount',
t2.discount = '$tot'
WHERE
t1.id = t2.sales_id AND
t1.pcode = '$pcode'
Note: I haven't tested it.