Mysql_fetch_array,assoc,从db获取数据后求助于数组的行

时间:2016-05-23 07:33:40

标签: php mysql

我正在执行一个查询

SELECT distinct dr.author as author , count(dr.author) as count FROM e_core_internet_analysis_58 dr,client_competitors,e_master_article_about WHERE `clientId`='173' AND dr.author!='' and client_competitors.clientId='173' and client_competitors.competitorId= e_master_article_about.company_id and e_master_article_about.id=dr.article_about_id and date(dr.publish_time) BETWEEN '2016-05-01' AND '2016-05-10' and dr.feed_class in ('Microblogs') and dr.company_name in ('Edelweiss - GIM','Edelweiss - WCM','India Infoline - WCM','Motilal Oswal - WCM','Emkay','KR Choksey','Motilal Oswal','Prabhudas Liladhar') group by author order by count desc limit 0,20

当我在数据库中运行此查询时,我得到的结果如下图所示。

enter image description here

当我使用以下代码获取数组时

$query_stack_st = "SELECT  distinct dr.author as author  , count(dr.author) as  count FROM e_core_internet_analysis_$sector_id dr,client_competitors,e_master_article_about WHERE `clientId`='".$client_id."'  AND dr.author!='' and client_competitors.clientId='".$client_id."' and client_competitors.competitorId= e_master_article_about.company_id and e_master_article_about.id=dr.article_about_id and date(dr.publish_time) BETWEEN '".$from."'   AND  '".$to."' and dr.feed_class in ('".rtrim($genre_id,"','")."') and dr.company_name in ('".rtrim($company_id,"','")."') group by author  order by count desc limit 0,20 ";

$res_stack_st = mysql_query($query_stack_st);
while($row =mysql_fetch_array($res_stack_st)) {
echo $row['author']."<br>";
}

我得到的结果为:

CNBC-TV18
Motilal Oswal Group
Save & Grow Money
NewsOnFire
OptionsTip
Sanjeev P Saikia
CNBC-TV18 News
Green Hedge Capital
Ajinkya Ambre
3NovicesChennai
India Market Updates
SanjayChowdary
Fin Thoughts
Investsmart - Sapan
moneycontrol
Shyam Sharma
Indian Stocks Update
Kairavi Shah
Ashutosh Tilak
RANJAN BUDA.

这是不期望的。按降序排序后,它还按降序对作者组进行排序。例如,

SanjayChowdary
Fin Thoughts

两者都有相同的数量但是 Fin思想应该是第一个,然后是Sanjay Chowdary,如图中所示。

这两个查询与我在浏览器中回显并在数据库中执行的查询完全相同。我尝试了不同的选项,例如mysql_fetch_assoc, mysql_fetch_arraymysql_fetch_row等,但仍然得到相同的结果。

有人可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

尝试在query.add auther列中更改此位置以按升序设置顺序。

   group by author  order by count desc,auther asc limit 0,20 ";