有可能在mysql上查询吗?

时间:2014-09-14 08:53:53

标签: php html mysql

可以查询吗?我有2张桌子。

tithing table
Tithing_ID = PK

tithingspayment table
TP_ID = PK
Tithing_ID = FK to tithings table "Tithing_ID"
Paid_Amount = decimal(19,2)
Paid_Month_Year = Date

Sample Record Table
tithings Table
 ___________
|Tithing_ID |
|_____5_____|

Tithingspayment_table
|TP_ID_|_Tithing_ID_|_PaidAmount__|__PaidMonthYear__|
|___1__|______5_____|____10.00____|_____Jan-2014____|
|___2__|______5_____|____10.00____|_____Feb-2014____|

我正在寻找的输出(如您所见,TP_ID,Tithing_ID,PaidAmount,PaidMonthYear按行复制。 我想使用mysql或PHP

查询
 Tithing_ID | TP_ID | Tithing_ID | Paid_Amount |   PaidMonthYear | TP_ID | Tithing_ID | Paid_Amount |  PaidMonthYear  | 
     2      |   1   |     5      |     10.00   |    Jan-2014     |   2   |      5     |     10.00   |     Feb-2014    |

编辑:因为我要求我不知道如何显示此输出

PaidMonthYear as Jan to Dec
Tithing_ID | Jan   | Feb   | Mar | Apr | May | Jun | Jul | Aug | Sept | Oct | Nov | Dec | Year |
     2     | 10.00 | 10.00 |  0  |  0  |  0  |  0  |  0  |  0  |   0  |  0  |  0  |  0  | 2014 |

因为我的代码显示此输出

Tithing_ID | Jan   | Feb   | Mar | Apr | May | Jun | Jul | Aug | Sept | Oct | Nov | Dec | Year |
     2     | 10.00 |   0   |  0  |  0  |  0  |  0  |  0  |  0  |   0  |  0  |  0  |  0  | 2014 |
     2     |   0   | 10.00 |  0  |  0  |  0  |  0  |  0  |  0  |   0  |  0  |  0  |  0  | 2014 |

1 个答案:

答案 0 :(得分:0)

伪代码就是这样的

$year_range= array('2013', '2014', '2015'); //<<give the year range here: say from 2013 to 2015
$months_range = array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC');

$data_to_display;
foreach($year in $year_range){
foreach($month in $months_range){
$query = mysql_query("SELECT * FROM Tithingspayment_table where PaidMonthYear = '{$month}-{$year}'");
$row = mysql_fetch_array($query);
$data_to_display[$year][$month] = $row['PaidAmount'];
}
}

//now you just need to display this $data_to_display in html table
//it is a 2 dimensional array that you need to properly display in your table
相关问题