dowbwrr3590709
dowbwrr3590709
2016-08-09 15:11

结合2个sql语句

已采纳

I wanted to ask can any one help me to join this two statement into one

SELECT * FROM worker_leave WHERE YEAR(yearAppplied) = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR))

And

SELECT * FROM worker_leave WHERE YEAR(yearAppplied) = YEAR(CURDATE())

I want to achieve counting the total leave a person have within current year and previous year total must not be more than 24 (Can be control at php side)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • doubei8541 doubei8541 5年前
    SELECT * FROM worker_leave  
    WHERE YEAR(yearAppplied) = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)) 
       OR YEAR(yearAppplied) = YEAR(CURDATE())
    

    This will give you combined results for both queries.

    点赞 评论 复制链接分享
  • dongyu9263 dongyu9263 5年前

    You can do

    SELECT * FROM worker_leave WHERE YEAR(yearAppplied) = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)) 
    UNION 
    SELECT * FROM worker_leave WHERE YEAR(yearAppplied) = YEAR(CURDATE())
    

    Make sure the columns returned by both queries are indentical [in structure]

    点赞 评论 复制链接分享

为你推荐