如何将当前月份与数据库列中的月份进行比较? [英] how to compare current month with the month in database column?

查看:378
本文介绍了如何将当前月份与数据库列中的月份进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hiii,我有两张桌子,分别是pd和fr。

pd包含pamount,pid

同时fr包含pid,month和frresult。

1pid基于其月份可以有很多结果,所以每个pid将有12个记录关于从1月到12月的frresult。



i尝试制作但它不起作用,下面是我的sql声明:



 select pid,case when when(pamount< = frrresult)
then'!'
end as notification
from pd,fr where MONTH(Month)= MONTH(GETDATE())





实际上,如果pd表中的pamount与fr表中的frrresult相等,且当前月份等于月份在fr表格中,这个通知会出来。



i已经在frr表中输入数据,其中pid1的5月份的结果小于pid1的pamount。但是当我执行它时,它没有任何输出。

我的代码有问题吗?

请帮帮我。先谢谢你。 :)

解决方案

你好朋友,请你试试下面的SQL:



  SELECT  PD.PID,' !'   AS  [通知] 
FROM PD INNER JOIN FR ON PD.PID = FR.PID
< span class =code-keyword> WHERE PD.PAMOUNT< = FR.FRRESULT
AND MONTH(FR.MONTH)= MONTH (GETDATE())


看起来查询中的表之间没有关系。此外,月来自哪里?我希望sql语句更像这样。

 选择 pid,
case 何时(pamount< = frrresult)
然后 ' !'
else NULL
结束 as 通知
来自 pd,fr
其中 pd.pid = fr.pid
MONTH(fr.Month)=月(GETDATE())





祝你好运!


hiii, i have 2 tables which is pd and fr.
pd contains pamount,pid
meanwhile fr contain pid, month, and frresult.
1pid can have many frresult based on its month, so each pid will have 12 record about frresult from january until december.

i tried to make it but it doesnt work, below is my sql statement:

select pid, case when (pamount<=frrresult)
          then '!'
         end as notification
from pd, fr where  MONTH(Month) = MONTH(GETDATE())



Actually if the pamount in pd table is less equal than frrresult in fr table and the current month is equal to the month in fr table, this notification will come out.

i already input the data in frr table where pid1's frresult for May is less than the pid1's pamount. But when i execute it , there is no any output from it.
is there sth wrong with my code?
please help me. thank you in advanced. :)

解决方案

Hello friend, can you please try the following SQL:

SELECT PD.PID, '!' AS [Notification]
FROM PD INNER JOIN FR ON PD.PID = FR.PID
WHERE PD.PAMOUNT <= FR.FRRESULT
AND MONTH(FR.MONTH) = MONTH(GETDATE())


Looks like there is no relation between the tables in your query. Also, where does Month come from? I would expect a sql statement more like this.

select pid, 
     case when (pamount<=frrresult)
           then '!'
           else NULL
          end as notification
    from pd, fr 
    where pd.pid = fr.pid 
      and MONTH(fr.Month) = MONTH(GETDATE())



Good luck!


这篇关于如何将当前月份与数据库列中的月份进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆