admin

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

sql

您好,我有这个专案,需要协助

$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


A | 10

B | 8

C | 7


或者


名称| 展示次数ASC


C | 7

B | 8

A | 10


感谢您的关注


阅读 170

收藏
2021-06-07

共1个答案

admin

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

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

2021-06-07