我已經嘗試了幾件事情,但我找不到一種方法來過濾此代碼,以便一次只顯示一年顯示。我已經添加了WHERE語句,語句,如:僅基於一年的SQL計數
DATE_FORMAT(show_date, '%Y') = 2013
但似乎沒有任何工作,它只是錯誤的代碼,並顯示什麼...
$mostplayed = mysqli_query($con,"SELECT tbl_shows.song_id, tbl_songs.song_name,
count(tbl_shows.song_id) as cnt, MID(song_name,1,35) as short_name, tbl_shows.show_date
FROM tbl_shows LEFT JOIN tbl_songs
ON tbl_shows.song_id = tbl_songs.song_id
GROUP by tbl_shows.song_id
ORDER by count(tbl_shows.song_id) desc
LIMIT 5");
echo "<table style='float: left; height:150px; margin-right:30px;'>";
echo "<tr style='background-color:transparent;'>";
echo "<td>";
echo "<h4>Most Played Songs:</h4>";
while($row = mysqli_fetch_array($mostplayed))
{
echo $row['short_name'];
echo " (" . $row['cnt'].")</br>";
}
echo "</td></tr></table>";
代碼本身返回宋朝之後括號中的數字。任何人都可以指點我如何只顯示一年的時間篩選出基於song_date年的數據?謝謝。任何幫助,將不勝感激。
上面的代碼工作,但抓住所有的數據由兩年... 下面的代碼返回Nothing
SELECT tbl_shows.song_id, tbl_shows.show_date, tbl_songs.song_name,
count(tbl_shows.song_id) as cnt, MID(song_name,1,35) as short_name
FROM tbl_shows LEFT JOIN tbl_songs WHERE show_date BETWEEN '2013-01-01' AND '2013-12-31'
ON tbl_shows.song_id = tbl_songs.song_id
GROUP by tbl_shows.song_id
ORDER by count(tbl_shows.song_id) desc
LIMIT 5
我拉在一起不久,一些數據點...
抱歉沒有工作,我的show_date是一個數據庫中的日期時間格式,一切似乎它會工作,任何其他的想法? – davejt
你能否詳細闡述一下*不起作用的意思?你得到一個錯誤,你沒有得到任何結果,你會得到意想不到的結果?它是什麼?恕我直言,您需要時間展示表格模式和樣品數據。否則它會在黑暗中不斷拍攝。 – peterm
另請參閱問題'show_date'和'song_date'中的兩個不同列。你究竟在嘗試使用哪一個,它屬於哪個表? – peterm