开发者

need help on select clause

开发者 https://www.devze.com 2023-01-23 17:08 出处:网络
i have a table named record which contains four column branch,account,name,month. now i want to find those data of this table which are present in current month, say month=3 but not present in previou

i have a table named record which contains four column branch,account,name,month. now i want to find those data of this table which are present in current month, say month=3 but not present in previous month, say month=2. how to do???

say i have these values: 4 214 jones 3 4 213 jim 3 4 123 nitu 2 4 213 jim 2

now i want to find the record row 4 214 jones 3 since it is the new record 开发者_如何学运维and it was not present in previous month's record. how to do?


On the SQL Server:

SELECT  branch,account,name,month FROM record WHERE Month = MONTH(GETDATE())

GETDATE() gets the current date and MONTH() gets the month out of a date...

You can use YEAR(), DAY() etc functions for such things.

On MySQL just replace MONTH(GETDATE()) with

MONTH(CURDATE())

To select a record that was not present in previous months just add this...

SELECT branch,account,name,month FROM record 
WHERE Month = MONTH(GETDATE()) 
AND NOT EXISTS 
(SELECT NULL FROM record rec1 WHERE rec1.account = record.account and Month < MONTH(GETDATE()))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号