Php and mysql get data by year and date wise! table structure?

When creating a page with exemplary details, I simply stuck with a really confusing situation. I really want to do this:

+----------------------------------------------------+ + Year | Month | Date | Total Gain + +----------------------------------------------------+ + 2003 | January | 26/01/2003 | +90 % + + 2003 | January | 27/01/2003 | +10 % + + 2003 | Feburary| 01/02/2003 | -29 % + + 2003 | Feburary| 15/02/2003 | +0.52 % + +----------------------------------------------------+ 

I really want so that I can list the names of the months and the year, such as mysql_query('SELECT month FROM mytable WHERE year="2003"');

But the problem is that he shows January twice. I want to display in January once and the link next to it, which will be moved to the next page. This will show the statistics for January.

+6
source share
2 answers

I think you are looking for a DISTINCT request, i.e.

 SELECT DISTINCT year, month FROM mytable 
+4
source

Maybe you should use the GROUP BY and also calculate the overall gain.

(Since the table structure is not specified, I will guess here)

 SELECT `Year`, `Month`, SUM(gain) AS 'Total Gain +' FROM table GROUP BY month, year; 
+5
source

Source: https://habr.com/ru/post/896088/


All Articles