计算某个日期之前的发生次数

时间:2016-02-01 09:18:09

标签: mysql sql count group-by

在MySQL中,我有一个包含以下列的记录表: ID,日期,voucher_id,为person_id

示例db

id         Date         voucher_id   person_id
-----      ------       ----------   ----------
0          2016-02-17   null         1
1          2016-02-18   null         1
2          2016-02-19   1            1
3          2016-02-20   2            2
4          2016-02-21   null         2
5          2016-02-22   3            2
6          2016-02-23   null         1
7          2016-02-24   4            1
8          2016-02-24   null         3
9          2016-02-25   null         3

我想知道使用优惠券的人员名单,并计算该人在该优惠券使用日之前在我的餐桌上记录的次数。

about table的结果是:

 Date         voucher_id   person_id    count_till_now
 ------       -------      ----------   --------------
 2016-02-19   1            1            2
 2016-02-20   2            2            0
 2016-02-22   3            2            1
 2016-02-24   4            1            4

哪个是生成此结果的最佳查询?

1 个答案:

答案 0 :(得分:2)

这样做的一种方法是使用相关子查询:

while True:
    #Welcome message
    name = input("Hello what is your name ?")
    print("Hi",name,"welcome to my code.")

    #Menus for first figure

    #Menu for rectangle
    def rectangle():
        length = int(input("\nWhat is the length ?"))
        width = int(input("What is the width ?"))
        print("The area is",length * width,"\n")
        answer_1 = (length * width)
    #Menu for square
    def square():
        length = int(input("\nWhat is the length of the side ?"))
        print("The area of the square is", length * length,"\n")
        answer_2 = (length * length)
    #Menu for triangle
    def triangle():
        base = int(input("\nWhat is the length of the base ?"))
        height = int(input("What is height of the figure ?"))
        print("The area of the right-angled triangle is", base * height * 0.5,"\n")
        answer_3 = (base * height * 0.5)
    #Main Menu   
    def menu():
        print("What would you like to do ?\n")
        print("1: Rectangle")
        print("2: Square")
        print("3: Right-angled Triangle\n")
        choice = int(input("Enter choice:"))
        if choice == 1:
            rectangle()         
        elif choice == 2:
            square()
        elif choice == 3:
            triangle()
    menu()

    #Menus for second figure

    #menu for rectangle
    def rectanglee():
        length = int(input("\nWhat is the length ?"))
        width = int(input("What is the width ?"))
        print("The area is",length * width,"\n")
        answer_4 = (length * width)
    #Menu for square
    def squaree():
        length = int(input("\nWhat is the length of the side ?"))
        print("The area of the square is", length * length,"\n")
        answer_5 = (length * length)
    #menu for triangle
    def trianglee():
        base = int(input("\nWhat is the length of the base ?"))
        height = int(input("What is height of the figure ?"))
        print("The area of the right-angled triangle is", base * height * 0.5,"\n")
        answer_6 = (base * height * 0.5)
    #Main menu
    def menuu():
        print("What would you like to do ?")
        print("1: Rectangle")
        print("2: Square")
        print("3: Right-angled Triangle\n")
        choice = int(input("Enter choice:"))
        if choice == 1:
            rectanglee()
        elif choice == 2:
            squaree()
        elif choice == 3:
            trianglee()
    menuu()

    #Answer_1 and Answer_2
    answer_1 = int(input("What was the first answer ?"))
    answer_2 = int(input("What was the second answer ?"))

    #Comparing


    while True:
        answer = input('Run again? (y/n): ')
        if answer in ("y", "n"):
            break
        print("Invalid input.")
    if answer == "y":
        continue
    else:
        print("Goodbye")
        break

Demo here

或者,您可以使用SELECT `Date`, voucher_id, person_id, (SELECT COUNT(*) FROM mytable AS t2 WHERE t2.person_id = t1.person_id AND t2.`Date` < t1.`Date`) AS count_till_now FROM mytable AS t1 WHERE t1.voucher_id IS NOT NULL

LEFT JOIN

Demo here