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..
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.
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;
I have two tables named 'register' and 'customer'. The register table looks like:
id customer_id purchase paid discount return previous_due
1 97 500 200 50 100
2 98 1500 700 150 500
3 97 70
4 99 900 900 0 1000
5 98 200
6 99 1200 1000
I want the SUM of each column by customer_id and automatically update the columns of 'customer' table. 'customer' table looks like:
customer_id tot_pur tot_paid tot_disc tot_return tot_due final_due
97 500 200 50 0 100 350
98
99
final_due column will be calculated like (tot_pur + tot_due) - (tot_paid + tot_disc + tot_return)
I am not good at mysql, so best and easy way will save me. Any help is appreciated. Thanks in advance.
Honestly, unless you need these sums to be on tap in a lightning-fast way, I might suggest just storing the values of each transaction separately, and then creating a view which aggregates by customer and finds the sums:
CREATE VIEW register_sums AS (
SELECT
customer_id,
SUM(purchase) AS tot_pur,
SUM(paid) AS tot_paid,
SUM(discount) AS tot_disc,
SUM(return) AS tot_return,
SUM(previous_due) AS tot_due,
SUM(purchase) + SUM(paid) - SUM(discount) - SUM(return) -
SUM(previous_due) AS final_due
FROM register
GROUP BY customer_id
)
I've to show the remaining quantity of books using eloquent query.
I can get total number of purchased books from this table
ordered_books
BOOKCODE[varchar(10)] QUNATITY[varchar(6)]
111 25
423 15
201 10
111 10
423 10
201 5
158 12
At first I've to sum the total books for each of the book code. Then from the another table I've to calculate the difference.
books_out
DISTRIBUTOR[varchar(50)] BOOKCODE[varchar(10)] QUNATITY[varchar(6)]
25 158 2
35 201 5
45 158 5
55 111 10
35 111 5
15 423 1
25 423 10
Again, from this table I've to calculate the total number of books taken by distributors then I can get the actual number of books present. How shall I write eloquent query for this scenario?
Try to use Raw query like this
==================================
$data=DB::select(DB::Raw("select t1.BOOKCODE,(total-selled) as available from (select BOOKCODE, SUM(QUNATITY) as total FROM `ordered_books` group by `BOOKCODE`) as t1,(SELECT BOOKCODE,SUM(QUNATITY) as selled FROM `books_out` group by `BOOKCODE`) as t2 where t1.BOOKCODE=t2.`BOOKCODE`"));
print_r($data);
//To get total books for each of the book code
SELECT BOOKCODE,SUM(QUNATITY) FROM `ordered_books` group by `BOOKCODE`
//To get total books_out for each of the book code
SELECT BOOKCODE,SUM(QUNATITY) FROM `books_out` group by `BOOKCODE`
Regarding your scenario, I think you require this query, please try it and comment whether it was helpful or not.
$countBooks = DB::select(DB::Raw(" SELECT A.TOTAL, IFNULL(B.SOLD,0) AS SOLD, (A.TOTAL - IFNULL(B.SOLD,0)) AS REMAINING FROM
(
select t1.BookID, SUM(t1.QUNATITY) AS TOTAL from ordered_books t1
GROUP BY t1.BookID) A
LEFT JOIN
(
SELECT BookCode, IFNULL(SUM(Quantity),0) AS SOLD FROM books_outs GROUP BY BookCode) B
ON A.BookID = B.BookCode
"));
dump($countBooks);
Also, better to display the name of books rather than code. It will be more user friendly. Thank You!!!