创建帖子的目的是,我根据我现在遇到的情况找不到案例的例子,我想从截止日期中取出超过1的金额而不是减去或等于零
如果已经存在相同的内容,我很抱歉,因为它找不到合适案例的示例。我只需要一个符合我案例的例子
我的数据库
id userid duedate status
1 lexter 2016-01-27 not yet paid
2 jemmy 2016-02-01 not yet paid
3 zaya 2016-03-02 already paid
4 randy 2016-08-09 not yet paid
5 max 2016-03-08 not yet paid
6 neo 2016-03-14 already paid
7 nemo 2016-01-09 not yet paid
这个mysql查询
SELECT duedate,
current_date() as datenow,
count(datediff(current_date(), duedate) as late_payment) as late_payment
FROM
mydb
WHERE
status='not yet paid'
我的查询只统计所有记录,尽管他小于零或大于零,我只希望记录大于零而尚未支付的记录将被计算。
答案 0 :(得分:1)
这称为条件计数,您可以通过sum()或count()来完成。您需要通过函数将case语句或if()放在组中。我将展示count()的示例:
<UserControl x:Class="WpfApplication3.CustomMarkerElement"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Ellipse
Fill="DarkKhaki"
Height="40"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="40" />
<Label
x:Name="lblDesc"
Content="TST"
Foreground="White"
FontWeight="Bold"
HorizontalAlignment="Left"
HorizontalContentAlignment="Center"
Margin="0,6"
VerticalAlignment="Top"
Width="40"/>
</Grid>
</UserControl>
您可以简化条件,因为SELECT duedate,
current_date() as datenow,
count(case when datediff(current_date(), duedate) >0 then 1 else null end) as late_payment_count
FROM
mydb
WHERE
status='not yet paid'
group by duedate, current_date()
实际上与datediff(current_date(), duedate) >0
相同。
如果您只对延迟付款的数量感兴趣,那么您可以进一步简化查询
current_date() > duedate