来自一个表的自定义输出数据

时间:2015-11-23 08:11:24

标签: php html mysql

我需要你的帮助和建议。我使用PHP和MySQL输出数据有问题。这是我的表:

enter image description here

我希望输出数据像这样:

1. Account: Budi, schedule_date = 2
2. Account: Randi, schedule_date = 1
3. Account: Ipul, schedule_date = 2
4. Account: Sari, schedule_date = 5

我的剧本

$date = $_GET['url'];
$sql = mysql_query("SELECT * FROM mst_schedule WHERE schedule_date LIKE '%$date'") or die (mysql_error());
while ($data1 = mysql_fetch_array($sql)) {
$sql2 = mysql_query("SELECT *, count(*) as schedule_date FROM mst_schedule WHERE schedule_account='$data1[schedule_account]'");
$data2 = mysql_fetch_assoc($sql2);
echo $data2['schedule_date'];
echo "<br>";
}

谢谢。

2 个答案:

答案 0 :(得分:1)

您可以尝试此查询:

SELECT schedule_account, count(*) as 'Cnt' FROM tbl_schedule GROUP BY schedule_account

并查看此fiddle

答案 1 :(得分:0)

在您的代码中使用此查询:

$sql1 = "SET @rank = 0";
$sql2  = "SELECT @rank := ( @rank +1 ) AS id, schedule_account AS Account, COUNT(schedule_date) AS schedule_date "
        . "FROM TBL_SCHEDULE GROUP BY schedule_account";