2

谁能帮我这个

脚本

这就是我想要实现的

桌子

id  name    tag_id  status      dob
== ======= ======== ========   ======== 
1   a       1        Married    02/23/1977
2   b       1        Single     02/23/1978
3   c       2        Single     02/23/1960
4   d       1        Married    02/23/1985
5   e       2        Married    02/23/1999
6   f       2        Diovice    02/23/1999
7   g       1        Married    02/28/1999
8   h       2        Married    02/28/1999
9   i       1        Married    03/12/1990

我很想用 mysql 查询得到现在/当前的生日庆祝者,这是我的代码:

SELECT * FROM table
WHERE (MONTH(STR_TO_DATE(dob, '%d/%m/%Y'))=MONTH(CURDATE())
    AND (DAY(STR_TO_DATE(dob, '%d/%m/%Y'))=DAY(CURDATE())))
    OR (DAY(LAST_DAY(STR_TO_DATE(dob, '%d/%m/%Y')))=29
    AND (DAY(STR_TO_DATE(bob, '%d/%m/%Y'))=29)
    AND (DAY(LAST_DAY(CURDATE()))=28))
    AND ( STATUS = 'Married' OR STATUS = 'Single' )
    AND tag_id = 1
ORDER BY id ASC

我的问题是上面的代码只是返回所有存在的 bithday 但我想要的是这样的:

结果:

1   a       1        Married    02/23/1977
2   b       1        Single     02/23/1978
4   d       1        Married    02/23/1985
7   g       1        Married    02/28/1999

错误结果(由上述查询产生):

1   a       1        Married    02/23/1977
2   b       1        Single     02/23/1978
4   d       1        Married    02/23/1985
7   g       1        Married    02/28/1999
8   h       2        Married    02/28/1999

而我想要的是,我只想要今天的生日庆祝者/ID = 1 的位置。就像我用第一个结果指定的那样。

4

3 回答 3

2
SELECT * FROM table
WHERE MONTH(dob) = MONTH(GETDATE()) AND DAY(dob) = DAY(GETDATE())
and tag_id = 1 and status in ('pending','active')

这应该有效。

于 2017-03-23T06:40:39.807 回答
0

查询给你错误的OR结果。(我不知道你为什么采取)

这是为前两个查询和其余查询创建一个或条件

因此,请将OR查询括在括号内。

然后,您将被AND tag_id = 1处决。

SELECT * FROM table WHERE 
    (MONTH(STR_TO_DATE(dob, '%d/%m/%Y'))=MONTH(CURDATE()) 
AND
    (DAY(STR_TO_DATE(dob, '%d/%m/%Y'))=DAY(CURDATE())))

 OR 

 (
    (DAY(LAST_DAY(STR_TO_DATE(dob, '%d/%m/%Y')))=29 
  AND 
    (DAY(STR_TO_DATE(bob, '%d/%m/%Y'))=29)
  AND
    (DAY(LAST_DAY(CURDATE()))=28))
  )

AND
    ( STATUS = 'ACTIVE' OR STATUS = 'PENDING' )
AND
    tag_id = 1
ORDER BY id ASC
于 2017-03-23T06:54:07.847 回答
0

这后来对我有用,它可能会帮助其他人,我很乐意看到这个答案的更多改进:

SELECT * FROM table WHERE MONTH(STR_TO_DATE(dob, '%d/%m/%Y')) = MONTH(NOW()) AND DAY(STR_TO_DATE(dob, '%d/%m/%Y')) = DAY(NOW()) AND TAG_ID = 2 AND STATUS IN ('single','married')
于 2017-03-29T20:52:13.957 回答