计算相同的数据库条目/ mysqli_num_rows

时间:2016-02-18 00:32:45

标签: count unique counting

在任何地方似乎都没有类似的问题。我需要这个循环遍历每个唯一值,然后踢出多少是相同的,但不知何故,我正在使用mysqli_num_rows错误。提前谢谢。

//This portion fetches unique entries that aren't empty
$query = "SELECT DISTINCT burger ";
$query .= "FROM newbob ";
$query .= "WHERE burger != '' ";
$query .= "ORDER BY burger ASC ";


$result = mysqli_query($dbc, $query);
if (!$result){
    die ("Database query failed.");
}

//and I want this portion to count how many there are that are the same, and
//just add it next to it.
while($entries = mysqli_fetch_assoc($result)) {
echo $entries["burger"];
$query2 = "SELECT burger ";
$query2 .= "FROM newbob ";
$query2 .= "WHERE burger = '$entries[burger]'";
$result2 = mysqli_query($dbc, $query2);
$rowcount = mysqli_num_rows($result2);

echo "( " . $rowcount . " )<br>";
}

编辑:我的查询以某种方式失败,我修复了查询中的空格,但是,我想知道我是否对查询的$entries[burger]部分做错了。

Edit2:可能会坐在这里并通过它打字!我只需要一些单引号。现在,当它击中撇号时,我失败了。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

是的,我正好按照自己的方式行事。它不一定很美,但我不确定如何做得更好。新的底部是

Sub GetBudgetTable(control As IRibbonControl)

Dim dbFilePath As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim cell As Range
Dim Year As String
Dim SQL As String

'For Each cell In Range("A1:A100")
    'If InStr(1, cell.Name, "SQL", vbTextCompare) > 0 Then

        Year = ActiveWorkbook.Sheets("Report").Range("Year").Value
        SQL = ActiveWorkbook.Sheets("Report").Range("BudgetSQL").Value

        'pulls budget
        dbFilePath = "H:\CORP\CFR-2011_to_Current\Budget\2015\Budget Variance\Budget Variance - Pivot\Test\More Tests\Administrative\Database\" & Year & " Budget.accdb"
        Set db = Access.DBEngine.OpenDatabase(dbFilePath, False, True)
        Set rs = db.OpenRecordset(SQL)

        ActiveWorkbook.Sheets("Budget Table").Range("A2:AJ80000").ClearContents
        ActiveWorkbook.Sheets("Budget Table").Range("A2").CopyFromRecordset rs
        db.Close

        'updates pivot table data source and refreshes pivot table
        DataSource = "Budget Table!R1C1:R" & ActiveWorkbook.Sheets("Budget Table").Range("A1").CurrentRegion.Rows.Count & "C" & _
            ActiveWorkbook.Sheets("Budget Table").Range("A1").CurrentRegion.Columns.Count

        ActiveWorkbook.Sheets("Report").PivotTables("Pivot").ChangePivotCache ActiveWorkbook.PivotCaches.Create(xlDatabase, _
            DataSource)
        ActiveWorkbook.Sheets("Report").PivotTables("Pivot").RefreshTable

End Sub