0

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

8
  • Try WHERE CONCAT(month,"-",year) >= "09 - 2017" AND GroupId = 29 Nov 29, 2019 at 11:53
  • No still all records are displayed
    – Questions
    Nov 29, 2019 at 11:59
  • Removed space WHERE CONCAT(Month,"-",Year) >= "9-2017" AND GroupId = 29 Nov 29, 2019 at 12:15
  • No, now the september months record is displayed
    – Questions
    Nov 29, 2019 at 12:17
  • your month column is INT and year is YEAR in DB Structure? Nov 29, 2019 at 12:22

1 Answer 1

0
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

6
  • Thankyou. This also works for me. Is it correct. SELECT * FROM groupsavingdetails WHERE ((groupsavingdetails.GroupId=29 and Year>=2017 and Month>9) OR (GroupId=29 and Year>2017AND Month>0))
    – Questions
    Nov 30, 2019 at 11:33
  • Both are same,but you are using twice ---> 'groupsavingdetails.GroupId=29' Nov 30, 2019 at 11:45
  • So can I use the one refered by you or the second one. Which is efficient?
    – Questions
    Nov 30, 2019 at 11:46
  • I think less condition in query is faster than other Nov 30, 2019 at 11:48
  • Hi , Now the situation is when I want to change September months record, then its closing balance will get changed and thus this new closing balance will be set to opening balance for the October month and so on. How should I do this?
    – Questions
    Nov 30, 2019 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.