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!!!
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;
My query is:
SELECT avg(result)
FROM `survey_result_full`
WHERE `comp_id`='$corow[id]'
AND rater_type='riwter'
AND survey_id='$survey'
AND rater_id in (
SELECT id
FROM raters
WHERE participate='$id'
AND `type`='$rt[id]'
)
Here I will get all the average of result:
id survey_id comp_id rater_id rater_type beh_id result
---- --------- ------- -------- ---------- ------ ------
6198 79 204 180 riwter 573 4
6576 79 204 181 riwter 573 4
6577 79 204 181 riwter 574 4
But I need to find the average of the averages for rows with identical beh_id.
If two rows have the same beh_id then I need to find the average of the result column of these two rows first then find average of all items.
If what you want is to get a result with [beh_id , average], then your query should be:
SELECT beh_id, avg(result)
FROM `survey_result_full`
WHERE `comp_id`='$corow[id]'
AND rater_type='riwter'
AND survey_id='$survey'
AND rater_id in (
SELECT id
FROM raters
WHERE participate='$id'
AND `type`='$rt[id]'
)
GROUP BY beh_id
maybe sth like
SELECT AVG(avg_results)
FROM (
SELECT srf.beh_id, AVG(srf.result) as avg_results
FROM `survey_result_full` srf, `raters` r
WHERE srf.`comp_id`= '$corow[id]'
AND srf.rater_type = 'riwter'
AND srf.survey_id = '$survey'
AND srf.rater_id = r.id
AND r.participate ='$id'
AND r.`type` = '$rt[id]'
GROUP BY srf.beh_id) AS avgs
since what you want seems to be an average of averages.
(I've also refactored a bit the query since what you want is a join on the rater table)
I want a resultset for this table:
ID Number_of_posts Number_of_user
1 100 21
2 23 34
as
ID Number_of_posts Number_of_user Number_of_posts_AND_Number_of_user
1 100 21 178
2 23 34 178
-----------------------------------------------
123 55
Is it possible to get the sum of two colums as another column/ as output in mysql?
To get cross-tab totals (horizontal and vertical):
select id,
number_of_posts as p,
number_of_users as u,
number_of_posts+number_of_users as p_and_u
from tbl
union all
select 99999 as id,
sum(number_of_posts) as p,
sum(number_of_users) as u,
sum(number_of_posts+number_of_users) as p_and_u
from tbl
order by 1
This will give you:
id p u p_and_u
----- --- --- -------
1 100 21 121
2 23 34 57
99999 123 55 178
You're complicating your query needlessly and using more memory that you have to. Pull the records in one query, then make another query to get the aggregates.
I know it doesn't answer your question, but it's what you should be doing instead. =)
SELECT id, number_of_posts, number_of_user,
(
SELECT SUM(number_of_posts + number_of_user)
FROM mytable
)
FROM mytable
SELECT SUM(Number_of_posts), SUM(Number_of_user) FROM table;
SELECT *,
(SELECT SUM(Number_of_posts) + SUM(Number_of_user) FROM TABLE) AS total
FROM table;
(Edit: Didn't originally notice it was the total total in the last column.)
Does MySQL support ROLLUP?
SELECT id,
SUM(Number_of_posts) Number_of_posts,
SUM(Number_of_user) Number_of_user,
SUM(Number_of_posts) + SUM(Number_of_user) Number_of_posts_AND_Number_of_user
FROM table
GROUP BY ROLLUP(id)
Edit: based on a quick search, in MySQL the last line might be GROUP BY id WITH ROLLUP.