MySQL返回求和值和一个虚拟列为(count-sum)

时间:2019-03-10 15:32:09

标签: mysql

我有一张桌子,如下所示: import turtle import winsound #window creation wn = turtle.Screen() wn.title("Classic Pong") wn.bgcolor("blue") wn.setup(width=800, height=600) # size in pixels wn.tracer(0) #stops auto-update of winodw to speed up the game #Score score_a = 0 score_b = 0 #Arena arena = turtle.Turtle() arena.shape("square") arena.color("orange") arena.shapesize(stretch_wid=30, stretch_len=0.1) arena.penup() arena.goto(0, 0) # Paddle A paddle_a=turtle.Turtle() #turtle object paddle_a.speed(0) #speed of animation paddle_a.shape("square") paddle_a.color("white") paddle_a.shapesize(stretch_wid=5, stretch_len=1) paddle_a.penup() #prevents the paddle from making a trace as it moves paddle_a.goto(-350, 0) #Paddle B paddle_b=turtle.Turtle() #turtle object paddle_b.speed(0) #speed of animation paddle_b.shape("square") paddle_b.color("white") paddle_b.shapesize(stretch_wid=5, stretch_len=1) paddle_b.penup() #prevents the paddle from making a trace as it moves paddle_b.goto(350, 0) #Ball ball=turtle.Turtle() #turtle object ball.speed(0) #speed of animation ball.shape("square") ball.color("white") ball.penup() #prevents the paddle from making a trace as it moves ball.goto(0, 0) ball.dx = 0.2 # delta x (the ball will move 1 position) ball.dy = -0.2 #Pen pen = turtle.Turtle() pen.speed(0) pen.color("Orange") pen.penup() pen.hideturtle() pen.goto(0, 260) pen.write("Player A: 0 Player B: 0", align="center", font=("Fixedsys", 24, "normal")) # Functions def paddle_a_up(): y = paddle_a.ycor() #ycor returns y coordinate y += 20 paddle_a.sety(y) #set y to the new y def paddle_a_down(): y = paddle_a.ycor() y -= 20 paddle_a.sety(y) def paddle_b_up(): y = paddle_b.ycor() y += 20 paddle_b.sety(y) def paddle_b_down(): y = paddle_b.ycor() y -= 20 paddle_b.sety(y) #Keyboard binding wn.listen() #listen for keyboard input wn.onkeypress(paddle_a_up, "w") #when w is pressed paddle_a_up() is called wn.onkeypress(paddle_a_down, "s") wn.onkeypress(paddle_b_up, "Up") #up key wn.onkeypress(paddle_b_down, "Down") #down key # Main game loop while True: wn.update() #everytime the loop runs it updates the screen #Moving the ball ball.setx(ball.xcor() + ball.dx) ball.sety(ball.ycor() + ball.dy) #Border checking #Top & bottom if ball.ycor() > 290: ball.sety(290) ball.dy *=-1 #reverses direction winsound.PlaySound("D:\Python\Pong_game_turtle\hit.wav", winsound.SND_ASYNC) elif ball.ycor() < -290: ball.sety(-290) ball.dy *=-1 winsound.PlaySound("D:\Python\Pong_game_turtle\hit.wav", winsound.SND_ASYNC) #Left and right if ball.xcor() > 390: ball.goto(0, 0) ball.dx *=-1 score_a +=1 pen.clear() pen.write("Player A: {} Player B: {}".format(score_a, score_b), align="center", font=("Fixedsys", 24, "normal")) elif ball.xcor() < -390: ball.goto(0, 0) ball.dx *=-1 score_b +=1 pen.clear() pen.write("Player A: {} Player B: {}".format(score_a, score_b), align="center", font=("Fixedsys", 24, "normal")) #Paddle and ball collisions #Right paddle #Checking if the ball is in the area in front of the paddle, right at the paddle's surface if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 40 and ball.ycor() > paddle_b.ycor() -40): ball.setx(340) ball.dx *= -1 winsound.PlaySound("D:\Python\Pong_game_turtle\hit.wav", winsound.SND_ASYNC) if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 40 and ball.ycor() > paddle_a.ycor() -40): ball.setx(-340) ball.dx *= -1 winsound.PlaySound("D:\Python\Pong_game_turtle\hit.wav", winsound.SND_ASYNC)

我想选择并返回3列log (log_id, log_success (bool), log_created) datesuccess,其中前者在表中不存在,最后按天汇总它们。

我创建了以下查询:

no_success

我可以通过此查询实现它吗?我的语法正确吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

如果要按此分组,必须选择DATE_FORMAT(log_created, '%Y-%m-%d')
您也可以使用no_success

获得SUM(abs(log_success - 1))计数器
SELECT
  DATE_FORMAT(log_created, '%Y-%m-%d') date,
  SUM(log_success) log_success,
  SUM(abs(log_success - 1)) no_success
  FROM send_log
GROUP BY date;

请参见demo