Hello I have two table in my database, customers and shops.
I want to get all customers and clarify that a customer is a debtor or not.
For this job I want to create a mysql query that get the customers and join the shop table.
I want to set condition for if shops.price_status column = 1 come and sum price value in shops table.
Otherwise the value of those row for price column in shop table equal to 0.
My shop table structure with some example data
id customer_id product_id price price_status
1 81 12 300 1
2 81 12 100 0
3 81 15 200 1
4 90 10 600 0
5 90 15 50 1
6 63 16 10 0
And my customer table (You should know some customers don't shop any product by I want to see them)
id name
63. Eva
64. Nva
81. Ali
82. Bill
90. Mosh
An I want to get this result
Eva. 0. // Because price status = 0
Nva. 0.
Ali. 500 // because have two row that prices status = 1
Bill. 0.
Mosh. 50. // Because have one row with price status = 1
this can do with [MySQL if][1]
SELECT
c.`name`,sum(if(s.price_status=1,s.price,0)) as price
FROM
customer AS c
LEFT JOIN
shops AS s
ON
c.id=s.customer_id
GROUP BY
c.id;
if you accept null value in price you can use
SELECT
c.`name`,sum(s.price_status*s.price) as price
FROM
customer AS c
LEFT JOIN
shops AS s
ON
c.id=s.customer_id
GROUP BY
c.id;
Related
I have read many topics but none have given me exactly what i need
I have 3 tables
products
id
product_name
1
Heineken
2
Budweiser
transaction
id
user_id
date
amount
32
4
01/23/2023
10000
45
2
01/23/2023
20000
57
4
01/23/2023
5000
transaction_details
id
transaction_id
product_id
1
32
1
2
32
2
3
45
2
4
45
1
5
57
1
Now how can i achieve this
SELECT FROM transaction WHERE user_id = 4 then use the results to SELECT FROM transaction_details and display it in a table using a single sql query
And when displaying the results from transaction_details i want to use product_name
This is the result i want
transaction_id
products_id and name
32
1. Heineken
32
2. Budweiser
57
1. Heineken
I have tried to select from transaction table where id is equal to 4 and i got two results, i don't know what to do next
Using Mysqli Join Query with select concat option in in your select query
Please Using This Way
select tran.id,CONCAT(pro.id,'.',pro.product_name) as products_id_and_name from transaction as tran join transaction_details as trand on tran.id = trand.transaction_id JOIN products as pro on pro.id = trand.product_id WHERE tran.user_id = 4;
the customer search is carried out by card number (see table A customer) and the code used by me is this:
$id = $_POST['card_number'];
SELECT e.*, sum(u.points) as points, sum(u.cost) as cost,
max(date_format(u.date_points, '%e/%c/%Y')) as data
FROM `customer` AS e
INNER JOIN `points` AS u ON e.id_customer = u.id_customer
where card_number='$id'
now I expose my tables and then I explain my problem
Table A Customer:
it contains the information of the members
id_customer
name
card_number
1
Luca
1234567890
2
Mark
9876543210
Table B Points:
it contains the points accumulated and the cost incurred
id_points
id_customer
points
cost
1
1
5
20
2
2
10
40
3
1
20
60
4
2
35
35
Result:
customer with id_customer 1 has a total of 25 points
customer with id_customer 2 has a total of 45 points
Table C deducted points:
it contains the points that will be deducted at the next purchase
id_deducted
id_customer
points
1
1
5
2
2
15
Result:
customer with id_customer 1 straight 5 points total remaining 20 points
customer with id_customer 2 climbs 15 points, total remaining 30 points
my question is this how can i make the sum of points from table B - the sum of points from table c? how can i add it in the select?
If you want a single request to get all info you need, you can make subrequests like this:
SELECT
c.*,
(
SELECT
SUM(p.points)
FROM
points p
WHERE
p.id_customer = c.id_customer
) AS points,
(
SELECT
SUM(d.points)
FROM
deducted_points d
WHERE
d.id_customer = c.id_customer
) AS deducted_points
FROM
customer c;
The result would be as follows
id_customer
name
card_number
points
deducted_points
1
Luca
1234567890
25
5
2
Mark
9876543210
45
15
But it would be very slow on large amounts of users and points changes I think. So I'd do select from users and then loop through them making individual requests to points and deducted_points tables.
Q : I am trouble to do following task in mysql query.
Task is get all products (If product is duplicate than sum of it's qty) and deduct wastage stock (If wastage product is duplicate than sum of it's qty).
I have two tables like,
1) manage_stock
2) manage_wastage
manage_stock
=> This table has p_id(Product ID) and many rows with product duplication's.
p_id p_name p_qty
1 Pro-1 10
2 Pro-2 15
3 Pro-3 8
1 Pro-1 15
manage_wastage
=> This table has p_id(Product ID) of manage_stock table. It is also many rows with product duplication's.
p_id w_qty
1 2
1 4
3 5
Desired Output
p_id p_name p_qty w_qty final_qty
1 Pro-1 20 6 14
2 Pro-2 15 0 15
3 Pro-3 8 5 3
Thank you very much.
You just have to compute the difference between the stock quantity and the wastage
SELECT s.p_id, s.p_name, SUM(p_qty), SUM(w_qty), SUM(p_qty) - SUM(w_qty) as final_qty
FROM manage_stock s
LEFT OUTER JOIN manage_wastage w
ON s.p_id = w.p_id
GROUP BY s.p_id, s.p_name
try this one
SELECT s.p_id, s.p_name, SUM(p_qty),SUM(ifnull(w_qty, 0)),SUM(p_qty - ifnull(w_qty, 0)) as total
FROM manage_stock s
left outer JOIN manage_wastage w
ON s.p_id = w.p_id
GROUP BY s.p_name
its work
SELECT ms.p_id ,GROUP_CONCAT(ms.p_name)p_name ,SUM(ms.p_qty) p_qty ,SUM(mw.w_qty) w_qty,SUM(ms.p_qty)-SUM(mw.w_qty) final_qty FROM manage_stock ms
INNER JOIN manage_wastage mw on ms.p_id =mw.p_id
GROUP BY ms.p_id
Try above code.
As p_name always unique with p_id GROUP_CONCAT() only returns single name.
I have multiple tables with orders and deliveries and I want to get only open orders (only those orders that do not have records in delivery table).
So, my tables look like:
Orders table (sh_comenzi):
id partner
1 Partner X
2 Partner Y
3 Partner Z
4 Partner Q
Order lines table (sh_comenzi_pos) where idc is the id of sh_comenzi table
id idc cPos quantity
1 1 1 5
2 1 2 10
3 1 3 20
4 2 1 10
5 2 2 15
6 3 1 10
7 3 2 5
8 3 3 8
9 4 1 15
The deliveries items table is (sh_delivery_items)
id idc cPos
1 1 1
2 1 3
3 2 2
4 3 1
5 3 2
6 3 3
The desired result should give me an output of open orders just like this:
id partner
1 Partner X
2 Partner Y
4 Partner Q
The result doesn't have to keep track o quantities, just on lines level. If one line from orders exists in sh_delivery_items then that line is closed.
I tried something like this:
SELECT DISTINCT sh_comenzi.id, partner FROM sh_comenzi
LEFT JOIN sh_comenzi_pos ON sh_comenzi.id = sh_comenzi_pos.idc
LEFT JOIN sh_delivery_items ON (sh_comenzi_pos.idc = sh_delivery_items.idc AND sh_comenzi_pos.cPos = sh_delivery_items.cPos)
WHERE sh_comenzi.id IS NOT NULL
ORDER BY sh_comenzi.id DESC
Could someone help me?
This is the query you need:
SELECT DISTINCT c.*
FROM sh_comenzi c
INNER JOIN sh_comenzi_pos p
ON c.id = p.idc
LEFT JOIN sh_delivery_items di # 'di' from 'delivery items'
ON p.idc = di.idc AND p.cPos = di.cPos
WHERE di.id IS NULL # keep only not-delivered items
How it works
It combines all the orders (table sh_comenzi) with their line items (table sh_comenzi_pos). The INNER JOIN will leave out the empty orders (if any); if you need them then use LEFT JOIN instead.
Next, each row (order, line item) is combined with the delivery information (table sh_delivery_items) using the pair of columns (idc, cPos). The LEFT JOIN ensures all the rows from the left side table (or result set) appear in the final result set; if a row from the right side table cannot be found to match the row from the left, a row full of NULLs is used instead. This happens for the line items that were not delivered yet (there is no record for them in sh_delivery_items).
Then, the WHERE clause keeps only the rows having NULLs in the di table (sh_delivery_items), i.e. the line items that were not delivered, together with the orders that own them.
Finally, SELECT DISTINCT c.* selects only the columns from the orders table (sh_comenzi) and DISTINCT ensures each order appear only once. Otherwise, each order appears once for each of its line items that was not delivered.
Complete the query yourself with the desired ORDER BY clause.
Hi I have table as per structure below:
Table1:
id rest_id rest_naam rest_min_order del_charges
----------------------------------------------------------
1 100 Rest1 300 50
2 211 Rest2 400 20
3 322 Rest3 100 60
Now i have another table which stores 'Min Order' and 'Delivery Charges' for different sectors / areas
Table2:
id rest_id del_area min_od min_del_chg
----------------------------------------------------
1 100 Area1 250 20
2 100 Area2 200 30
3 322 Area3 100 50
4 322 Area4 150 0
Now when i will take input from user for his Area and restaurant name then minimum order and delivery shall be displayed as per the records in Table2 to the user.
I used query as below:
SELECT * FROM Table1 t1 where rest_naam = 'User INput' left join Table2 t2 on
t2.rest_id=t1.rest_id;
With this Delivery charges and Minimum order are getting displayed from both tables. For eg. For Rest=100, user is able to see min order as 300 and 250 when he selects area1.
I want that if Area1 has delivery charges in Table2 then Table1 delivery charges shall not be displayed..