Hello Developers/Programmers
I am working on withdrawal function on my website.
So it goes like this
I need to total the 3 tables i have by User ID with status of '1' ,and Subtract the Inputed amount by the User Requested the Withdrawal
These are my 3 tables
tbl_bonus_1
id | amount | user_id | status
1 20 1 1
2 20 1 1
3 20 3 1
tbl_bonus_2
id | amount | user_id | status
1 30 1 1
2 30 1 1
3 30 3 1
tbl_bonus_3
id | amount | user_id | status
1 40 1 1
2 40 1 1
3 40 3 1
Now I need to get all that 3 tables by USER ID and get the total of it.
After getting the total i need to subtract the Inputted quantity of the USER
and update the status to 0 so that the user cant withdraw again.
Im using Codeigniter 3.1.5
select user_id, sum(amount)
from
(select * from tbl_bonus_1
union
select * from tbl_bonus_2
union
select * from tbl_bonus_3) tt
where status = 1
group by user_id
DEMO:
http://sqlfiddle.com/#!9/7f1807e/1
And UPDATE (single user):
UPDATE tbl_bonus_1 t1 INNER JOIN tbl_bonus_2 t2
ON t1.user_id = t2.user_id
INNER JOIN tbl_bonus_3 t3
ON t1.user_id = t3.user_id
SET t1.amount = 0, t2.amount = 0, t3.amount = 0
WHERE t1.user_id = 1;
Realistically, you don't want to subtract from these tables if you want to manage a balance. You need to add a 4th table that is withdrawal amount, so you can capture the transactions. If you have a total of 160 across 3 tables, and the user withdrawals 150, how would you determine which to decrement.
I would suggest actually consolidating all of these into 1 trasaction table, and capture the amounts there.
So to get what you need you will need to leverage variables which will need to be passed to the query. Below will give you what you are asking for. That being said, this is not the correct way to do this. Also, there is no way to determine which of the 3 balances you want to subtract the withdraw from. This is just not how transaction ledgers work and for a lot of reasons I am not going to get in to right now. At the bottom of this answer is how I suggest you should build your table. You will be able to get information with more easy while capturing more/better data.
How to get data from current structure:
set #withdraw = 150.00, #user = 1;
select user_id, sum(amount) as prevBalance
, #remainingBalance := if(#user = user_id,sum(amount)-#withdraw,sum(amount)) as remainingBalance
from
(select * from tbl_bonus_1
union
select * from tbl_bonus_2
union
select * from tbl_bonus_3) balance
group by user_id;
How you should build your schema:
CREATE TABLE ledger (id int NOT NULL AUTO_INCREMENT
,user_id int
, amount decimal(5,2)
, transaction_type varchar(20)
,PRIMARY KEY (ID));
INSERT INTO ledger VALUES
(null,1,20,'Bonus1'),
(null,1,20,'Bonus1'),
(null,3,20,'Bonus1'),
(null,1,30,'Bonus2'),
(null,1,30,'Bonus2'),
(null,3,30,'Bonus2'),
(null,1,40,'Bonus3'),
(null,1,40,'Bonus3'),
(null,3,40,'Bonus3'),
(null,1,-150,'Withdraw')
;
Then all you would need to do is run the following query.
select user_id, sum(amount) balance from ledger
group by user_id;
Related
I'm trying to bulk-get all of the latest payments for a group of user ids, but I've been struggling with getting all of them under a unified query
I went with:
SELECT t1.*
FROM movements t1 LEFT JOIN movements t2
ON (t1.user = t2.user AND t1.id < t2.id)
WHERE t2.id IS NULL
AND t1.user IN ({$ids}) AND t1.type='payment' AND t1.concept!='4' AND t1.confirmed
...which worked to an extent, but some entries were being left out. I extended it to
ON (t1.user = t2.user AND t1.id < t2.id)
WHERE t2.id IS NULL AND t2.date IS NULL
and that yielded more results, but some of them weren't being selected still.
Here are two samples where the query will not yield anything
id user concept date type confirmed
---------------------------------------------------------------------------------
29755 107 3 2022-06-12 00:01:00 payment 1
31257 107 3 2022-07-12 00:00:00 payment 1
32189 107 3 2022-08-12 00:00:00 payment 1
32460 107 COMISSION BALANCE 2022-08-23 10:50:50 comission
id user concept date type confirmed
---------------------------------------------------------------------------------
27298 8408 3 2022-03-11 08:44:53 40 payment
28446 8408 3 2022-03-11 00:01:00 40 payment 1
28447 8408 3 2022-04-19 17:22:42 40 payment
Using the "crude" alternative does, obviously.
SELECT * FROM movements WHERE user=107 AND (etc) ORDER BY id DESC LIMIT 1
id user concept date type confirmed
---------------------------------------------------------------------------------
32189 107 3 2022-08-12 00:00:00 payment 1
Since I have the ids, I could simply do a foreach for every $user and make a million individual calls, but I'd rather avoid that.
I can tell there's something off with the "topmost" rows not meeting the full criteria, but I have no clue on how to tell SQL to get me the ones that do.
How could I achieve this in a clean way?
To select the row with the highest id for each user that matches the additional criteria, even if there's a row with a higher id that doesn't match the additional criteria, you need to add to your on clause:
and t2.type='payment' AND t2.concept!='4' AND t2.confirmed
You may also try the following:
Select T.id, T.user, T.concept, T.date, T.type, T.confirmed
From
movements T
Join
(
Select MAX(id) lastid, user
From movements
Where user IN (107, 8408) And type='payment' And concept!='4' And confirmed
Group By user
) D
On T.id = D.lastid
See a demo.
select id
,user
,concept
,date
,type
,confirmed
from (
select *
,row_number() over(partition by user order by id desc) as rn
from t
where confirmed = 1
) t
where rn = 1
id
user
concept
date
type
confirmed
32189
107
3
2022-08-12 00:00:00
payment
1
28446
8408
3
2022-03-11 00:01:00 40
payment
1
Fiddle
I have two tables with the name of customers and installments.i.e.
Customer Table:
id | name |contact |address
1 | xyz |33333333|abc
2 | xrz |33322333|abcd
Installments Table:
id | customer_id |amount_paid |amount_left | date
1 | 1 |2000 |3000 | 13/05/2017
2 | 1 |2000 |1000 | 13/06/2017
Now, I want to fetch the latest installment row from installments table for every user, I can use that with the following query,
SELECT * FROM installments WHERE customer_id=1 ORDER BY `id` DESC LIMIT 1
Now, the issue is that I want to do it for all the customer ids. I tried sub query thing but that doesn't supports multiple rows. I tried to store all customer IDs in an array in PHP and used "IN" but because the number of customers is more than 3000, it takes too long and returns an error of execution time exceeded.
Any kind of help or tip will be really appreciated. Thanks
SELECT
Customers.CustomerId,
Customers.Name,
Customers.Contact,
Customers.Address,
Installments.InstallmentId,
Installments.AmountPaid,
Installments.AmountLeft,
Installments.Date
FROM
Customers
INNER JOIN
(
SELECT
MAX( InstallmentId ) AS MaxInstallmentId,
CustomerId
FROM
Installments
GROUP BY
CustomerId
) AS LatestInstallments ON Customers.CustomerId = LatestInstallments.CustomerId
INNER JOIN Installments ON LatestInstallments.MaxInstallmentId = Installments.InstallmentId
you can do something like this
SELECT c.*,I.* FROM Customer_Table c LEFT JOIN Installments_Table I ON c.id=I.customer_id ORDER BY c.id DESC LIMIT 1
If You wants to add limit of list then only set limit else leave limit part. Untill I got Your Question this will be help you. else your problem can be something else.
SELECT cust.*,inst_disp.* FROM Customer AS cust
LEFT JOIN (
SELECT MAX(id) AS in_id, customer_id
FROM installments
GROUP BY customer_id
) inst
ON inst.customer_id = cust.id
LEFT JOIN installments as inst_disp ON inst_disp.id = inst.in_id
I have a table which stores user items, the two key columns which I would like to use in this query are user_id and item_id. The id field in the example is not needed but just added to show these aren't the only two columns in the table.
----------------------
id user_id item_id
----------------------
1 1 324
2 1 324
3 3 324
4 2 230
5 4 324
The query which I would like to construct should return the top 10 users who have the most items with a specific item id.
So for example if I wanted to run the query against the item ID 324 I should get the following result.
-------------------
user_id item_count
-------------------
1 2
3 1
4 1
2 0
try this
select user_id , count(*) as item_count from table
where item_id = 324 group by user_id order by item_count desc limit 10
limit 10 will show you the top 10 users and order by desc sort from high to low.
However, the above query will not give you the 0 count as per your question. If you really want the zero count you can try this: (assuming your table name is userlist)
SELECT distinct user_id,
(select
count(*) from `userlist`
where user_id=u.user_id and item_id=324
) as item_count FROM `userlist` u
order by item_count desc
I couldn't create the database in my local, but I think this will do the trick
SELECT user_id, COUNT(item_id) as item_count
FROM TABLE_NAME
WHERE item_id = 324
GROUP BY item_id
ORDER BY item_count;
I have a simple table called users with the following data:
id | hops
1 | 3
2 | 1
3 | 5
4 | 2
5 | 4
6 | 5
I want to select the number of hops of any given id that I specify and also select the next and previous ids according to the number of hops sorted from highest to lowest.
To explain more I use the following query:
SELECT * FROM test WHERE id = 1
OR (id > 1 AND hops >= (SELECT hops FROM test WHERE id= 1) )
OR (id < 1 AND hops <= (SELECT hops FROM test WHERE id= 1) )
LIMIT 3
So in the above query I tried to get id=1, next id with the same or higher number of hops, and the previous id with the same or lower number of hops.
This is the result i get:
id | hops
1 | 3
3 | 5
5 | 4
As you can see it selected id=1 and two higher ids although I want only one higher id and one lower id. So, in this case the result should be like this instead:
id | hops
1 | 3
3 | 5
As there is no lower id than 1, so nothing lower to fit the criteria and selects only 1 higher id. The wrong result is because of using LIMIT 3 but I can't use a LIMIT for each condition. So don't know how to approach this at all.
Have another question, would using the sub-query
"SELECT hops FROM test WHERE id= 1"
slow down the server on a large scale?? I heard that it's not preferable to use sub-queries but have no other way to get this number except using a separate query.
Thanks
here you go, change the order by ID according to your liking...you didn't say if you wanted the closest number of hops or the closest ID, just one greater or lower
SELECT * FROM test
WHERE id IN (1,(
SELECT id FROM test WHERE id > 1 AND hops >= (
SELECT hops FROM test WHERE id = 1
) ORDER BY id LIMIT 1
), (
SELECT id FROM test WHERE id < 1 AND hops <= (
SELECT hops FROM test WHERE id = 1
) ORDER BY id DESC LIMIT 1
))
If I understand your question correctly, I believe the following will work.
-- Previous Rec
SELECT t2.*
FROM test as t1
JOIN test as t2 ON t2.hop <= t1.id
WHERE t1.id = 1
ORDER BY t2.id DESC
LIMIT 1
UNION ALL
-- Current Rec
SELECT *
FROM test as t
WHERE id = 1
UNION ALL
-- Following Rec
SELECT t2.*
FROM test as t1
JOIN test as t2 ON t2.id >= t1.hop
WHERE t1.id = 1
ORDER BY t2.id ASC
LIMIT 1
Im having the below sql table structure and im trying to grab values from 2 tables using codeigniter.
table: salesman
id sales_name ref_id
1 kevin 174
2 mike 574
3 nick 777
table: sales_report
id salesman_id product purchased_date dispatched
1 2 BF0214 04-November-2011 Yes
2 2 CF0474 09-November-2011 No
3 2 BF0111 10-November-2011 No
4 3 BF0714 15-November-2011 Yes
5 3 BF0435 15-November-2011 Yes
6 2 BF0335 18-November-2011 Yes
7 1 BF0714 22-November-2011 Yes
8 1 BF0335 25-November-2011 Yes
im passing the salesman_id to the model to grab and display the values in my view.
my html table is as below
Ref ID | Salesman Name | Last product Sold | Sold Date | Dispatched Status
the problem that im having how can query to get the sales_name and ref_id from the salesman table and get the most recent product name, dispatched and purchase_date from from the sales_report table?
eg:
Ref ID | Salesman Name | Last product Sold | Sold Date | Dispatched Status
174 kevin BF0335 25-November-2011 Yes
574 mike BF0335 18-November-2011 Yes
777 nick BF0435 15-November-2011 Yes
Use
GROUP BY
ORDER BY
for getting this done
You need to use SELECT values from the sales_report, LEFT JOIN salesman to get in the sales_name and ref_id, ORDER BY the purchased_date (DESC to get the most recent one first instead of the oldest first), and GROUP BY the salesman.id to get it to only return one row per salesman.
SELECT * FROM sales_report LEFT JOIN salesman ON sales_report.salesman_id = salesman.id ORDER BY purchased_date DESC GROUP BY salesman.id
You should find last product by two criteras: its sold date and by its id; because some products may be sold in one day. So, try this one -
SELECT s.ref_id, s.sales_name, sr.product, sr.purchased_date, sr.dispatched FROM salesman s
JOIN (
SELECT salesman_id, product, purchased_date, dispatched FROM (
SELECT salesman_id, product, purchased_date, dispatched, IF(#salesman_id = salesman_id, #i := #i + 1, #i := 1) n, #salesman_id := salesman_id
FROM sales_report, (SELECT #i:= 0, #salesman_id = NULL) vars
ORDER BY salesman_id ASC, purchased_date DESC, id DESC) t
WHERE t.n = 1
) sr
ON s.id = sr.salesman_id;