从SQL输出中仅读取一列作为PHP中的数值数组 [英] Reading only one column from SQL output as a numerical array in PHP

查看:153
本文介绍了从SQL输出中仅读取一列作为PHP中的数值数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行以下查询:

  SELECT tagID, 
         COUNT(*) AS TotalOccurrences
    FROM coupon_tags
GROUP BY tagID
ORDER BY TotalOccurrences DESC 
   LIMIT 10

它返回这样的输出:

tagID  TotalOccurrences 
------------------------
7      9
2      8
1      3
6      2
3      1
4      1
5      1
8      1

我不能做 mysql_fetch_array(mysql_query($ thatQuery); ,因为它有两列数据,而任何数组都被拖拉了,就像垃圾一样。我如何进一步简化该查询,使它仍然是一列仍然排序的数据,所以更容易在数组中使用?或者也许我使用了错误的PHP / MySQL函数(尽管我仔细检查了所有内容)?

I can't do a mysql_fetch_array(mysql_query($thatQuery); because it has two columns of data and any array pulled looks like garbage. How can I further streamline that query to a single column of still-sorted data, so it's easier to work with in an array? Or maybe I am using the wrong PHP/MySQL function (although I looked through them all)?

编辑:我发现查询在phpMyAdmin中可以正常工作,但是当我尝试使用phpMyAdmin查询时失败 mysql_query()

I've found out that the query will work fine in phpMyAdmin but it fails when I try to query with mysql_query().

我的php代码:

$tagSQL = mysql_query($selectorSQL);
        if (!$tagSQL) die("query failed");  //fails here
            while ($tSrow = mysql_fetch_assoc($tagSQL)) {
                var_dump($tSrow);
            }


推荐答案

您可以这样做将查询简化为仍然排序的数据的单列。

You can do it like this to "streamline that query to a single column of still-sorted data".

SELECT tagID            
FROM coupon_tags
GROUP BY tagID
ORDER BY COUNT(tagID) DESC 
LIMIT 10

只需确保对单个列使用计数,而不要对所有内容进行计数,这将极大地影响性能。

Just make sure to use count on a single column instead of counting everything, this will greatly affect performance.

这篇关于从SQL输出中仅读取一列作为PHP中的数值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆