Your Location is: Home > Php

How to get records greater than selected year and month when the year is greater and month is less than specified one in Yii2

From: Yaren View: 2059 Questions 

Question

I am using Yii2 basic. I have a table called groupsavingdetails. In that table I have records for a specific group with Id 29 and from year 2017 and Months August, September, October, November and December. Also there are records for the same group for year 2018 January and March.

The situation is I want to update record whose year is 2017 and month is September. Now I want to get all the records greater than September 2017 record. Total 5 records should I get but only 3 records are fetched by the following query.

SELECT * FROM groupsavingdetails WHERE groupsavingdetails.GroupId=29 and Year>=2017 and Month>9

Below is the image of table contaning records. So I should get records from Id 47,48,49,51,52. How should I get it.

enter image description here

Best answer

SELECT * FROM groupsavingdetails WHERE 
groupsavingdetails.GroupId=29 
and ( Year > 2017 or (Year>=2017 and Month > 9) )

In This query

if year is greater than 2017 it will display

and year is 2017 and month is greater than 9 also display