0

I want to display one paid status(1) column and then 3 unpaid(0) status column respectively.

Paid status is taken based on Expirydate, the date greater than now is considered paid status and date which is less than expiry date is considered unpaid status. Output can be based on Entry date.

My Table:

Id Expirydate Entrydate
1 2022-12-10 2022-10-11
2 2022-12-09 2022-09-01
3 2022-10-10 2022-10-18
4 0000-00-00 2022-10-17
5 2022-09-08 2022-10-01
6 0000-00-00 2022-10-15
7 0000-00-00 2022-09-09
8 2022-11-30 2022-09-10

Output would be:

Id Expirydate Entrydate Status
1 2022-12-10 2022-10-11 1
3 2022-10-10 2022-10-18 0
4 0000-00-00 2022-10-17 0
5 2022-09-08 2022-10-01 0
2 2022-12-09 2022-09-01 1
6 0000-00-00 2022-10-15 0
7 0000-00-00 2022-09-09 0
8 2022-11-30 2022-09-10 1

NOTE: Considering now() as 2022-10-18. Also last there was only 2 unpaid status so the rest paid status would be shown.

1st attempt to calculate status:

SELECT table.* , CASE WHEN `expirydate` >= NOW() THEN "1" ELSE "0" END AS paidstatus 
 FROM table ORDER BY paidstatus DESC

Thanks.

4
  • What is your MySQL version? And what is the datatype for Expirydate column? Additionally, where exactly the ordering issue, MySQL or you end output, web app perhaps? Commented Oct 18, 2022 at 6:25
  • 'I want to display one paid status(1) column and then 3 unpaid(0) status column respectively.' - I don't get this is there some relationship between the rows you haven't told us? Commented Oct 18, 2022 at 7:37
  • Mysql version: 10.4.20-MariaDB. We are working in Mysql and Laravel 8. Regarding the relationship, there is no relationship with the rows. Our clients would like to display 1 paid row and then 3 unpaid row then the display repeats. Commented Oct 19, 2022 at 12:12
  • "Our clients would like to display 1 paid row and then 3 unpaid row" .. that might be achievable but I don't understand how to determine what is the next value in the order after a 1 paid row? I mean, look at your expected output.. The first EntryDate='2022-10-11' is with paidstatus=1 then the next 3 values ordered are still logical somehow but after the next paidstatus=1 it becomes .... Commented Oct 20, 2022 at 1:33

1 Answer 1

0

You can do this to solve your problem.

SELECT *, ( table.Expirydate > NOW() ) as `Paid_Status` FROM table ORDER BY `Paid_Status` DESC;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.