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

$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要获取印象,我需要按印象对名称进行排序
姓名|印象描述


C | 7

姓名|印象ASC
C | 7


谢谢你的关注

最佳答案

我将加上几乎与你上一个问题相同的答案。您可以在一个join而不是单独的查询中完成这一切;

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

An SQLfiddle to test with

关于php - sql1获取名称,sql2获取印象,我需要按印象对名称进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16536571/

10-10 18:03