Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

sql - PHP MySQL Admin Query Assistance

I need help with a Query to get the transaction per user, I am working my way on the queries, still learning, any assistance will be appreciated.

Below Query is getting what I need for another report, pretty much transactions per Day, working good:

SELECT
date_generator.date as the_date,
IFNULL(COUNT(transactions.transaction_id), 0) as count
from (
select DATE_ADD('2021-01-01', INTERVAL (@i:=@i+1)-1 DAY) as date
from information_schema.columns,(SELECT @i:=0) gen_sub
where DATE_ADD('2021-01-01',INTERVAL @i DAY) BETWEEN '2021-01-01' AND '2021-01-12'
) date_generator
left join transactions on DATE(edited) = date_generator.date
GROUP BY date

Generates Data:
the_date count
2021-01-01 0
2021-01-02 0
2021-01-03 0
2021-01-04 12
2021-01-05 24
2021-01-06 0
2021-01-07 11
2021-01-08 0
2021-01-09 0
2021-01-10 5
2021-01-11 8
2021-01-12 3

My challenge, is that now I want the same data but per an specific username... in the same transaction table, I do have a column call username, which generates the transaction. I was playing with the Query but no luck, thanks for you assistance again.

Looking to Get... Where username = 'Test':
the_date count
2021-01-01 0
2021-01-02 0
2021-01-03 0
2021-01-04 1
2021-01-05 0
2021-01-06 0
2021-01-07 1
2021-01-08 0
2021-01-09 0
2021-01-10 0
2021-01-11 1
2021-01-12 2


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After digging around, and no answers... finally got what needed in another forum, in case someone else need this:

SELECT date_generator.date as the_date, username, IFNULL(COUNT(transactions.transaction_id), 0) as count from ( select DATE_ADD('2021-01-01', INTERVAL (@i:=@i+1)-1 DAY) as date from information_schema.columns,(SELECT @i:=0) gen_sub where DATE_ADD('2021-01-01',INTERVAL @i DAY) BETWEEN '2021-01-01' AND '2021-01-12' ) date_generator left join transactions on DATE(edited) = date_generator.date AND username= 'Test' GROUP BY date, username


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...