2017-06-22 92 views
-1

我在查詢我的數據庫獲取其中的所有信息,但是我想要做的是抓取所有具有'x'的條目並對它們進行分組,然後抓取所有具有'y'的條目並對它們進行分組。SQL從查詢結果中獲取物品的數量

我最好試圖將每個數字月份值映射到一個JavaScript按鈕。當你點擊第1個月按鈕時,你會得到所有分組顯示在JavaScript表中的第1個條目,以及有多少條目的計數。然後,表格將循環顯示條目並將其打印在屏幕上。

我抓住項目這個說法

$myresult = customfunction($getCred, "Select * From Test_Data"); 

多個月來的列的名稱是NumericMonths,(整數) 例如可以說,我希望將所有的「月」 這是JavaScript表我已經安裝

<div class="container"> 
<ul class="nav nav-pills"> 
<li class="active"><a data-toggle="pill" href="#Month1">Month 1(this is where the count of items should go)</a></li> 
<li ><a data-toggle="pill" href="#Month2">Month 2(this is where the count of items should go)</a></li> 
<li><a data-toggle="pill" href="#Month3">Month 3(this is where the count of items should go)</a></li> 
<li><a data-toggle="pill" href="#Month4">Month 4(this is where the count of items should go)</a></li> 
<li><a data-toggle="pill" href="#Month5">Month 5(this is where the count of items should go)</a></li> 
<li><a data-toggle="pill" href="#Month6">Month 6(this is where the count of items should go)</a></li> 
<li><a data-toggle="pill" href="#Month7">Month 7(this is where the count of items should go)</a></li> 

然後儘管這取決於WH的內容,我想循環ICH按鈕用循環

while ($row = custom_fetch_array($res)){ 

#My code here to generate a table such as $row["Name"] etc etc 

}; 
+0

請閱讀[我可以問哪些主題](http://stackoverflow.com/help/on-topic) 和[如何提出一個好問題](http://stackoverflow.com/help/how - 問) 和[完美的問題](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) 以及如何創建[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve) ** SO不是**免費的編碼服務___我們嘗試修復您的代碼,我們不寫您的代碼____ – RiggsFolly

回答

0

拿起這將讓你兩列,即rows_per_month包含的項目編號爲一個月,NumericMonths包含月份數。結果中最多有12行。

Select count(*) as rows_per_month, NumericMonths From Test_Data group by NumericMonths 

讀取行的請求的一個月:

Select * from Test_Data where NumericMonths = requested_month 
0

好吧,這是一個有點棘手,制定出你的設置是,但從來沒有少讓一展身手。 說實話,讓您的PHP生活更輕鬆的計數。

所以使用一些SQL的計數

SELECT COUNT(1) FROM Test_Data WHERE NumbericMonths = 1; 

如果有很多,那麼這將使它更容易。

第2步 - 數據。

我會做這樣的事情。 (psudo代碼)

$array = []; 

$query = "SELECT * FROM Test_data " 

foreach($row in $results) { 
    $array[$row[month]] = $array[$row[month]] . '<tr><td>'.$row['something'].'</td></tr>'; 
} 

foreach($a in $array) { 
     echo $a 
} 

這是很簡單的,我不知道我做了正確的在foreach - 已經有一段時間我做了PHP,但這個想法是存在的。