2

你好我有这个项目,我需要帮助我

$sql1 = "SELECT COUNT(*) as num FROM table1 where column1='1'";

    $total_Results = mysql_fetch_array(mysql_query($sql1));

    $total_Results = $total_Results[num];

    while($row = mysql_fetch_array($sql1))

        {

$sql2="select distinct column1 from table2 where column2='".$row['id']."' and left(date,10) BETWEEN '".$datefrom."' AND '".$dateto."'";

$res=mysql_query($sql2);

}

sql1 获取名称,sql2 获取印象数,我需要按印象数对名称进行排序


姓名 | 展示次数 DESC


一个 | 10

乙| 8

C | 7


或者


姓名 | 展示次数 ASC


C | 7

乙| 8

一个 | 10


感谢您的关注

4

2 回答 2

0

您还需要在 sql1 查询中选择 id

$sql1 = "SELECT id,COUNT(*) as num FROM table1 where column1='1'";

否则您将不会在结果中获得 id 或者您可以选择所有字段 + 计数 (*)

 $sql1 = "SELECT *,COUNT(*) as num FROM table1 where column1='1'";
于 2013-05-14T07:04:12.553 回答
0

我将添加与您上一个问题几乎相同的答案。您可以在连接中完成所有操作,而不是单独的查询;

SELECT t1.id as name, COUNT(DISTINCT t2.column1) Impressions
FROM table1 t1
LEFT JOIN table2 t2
  ON t1.id=t2.column2
WHERE t1.column1 = 1
  AND LEFT(date,10) BETWEEN '2013-01-01' AND '2013-12-31'
GROUP BY t1.id
ORDER BY Impressions DESC

一个用于测试的 SQLfiddle

于 2013-05-14T08:35:40.667 回答