mysql select,count,left join和show

时间:2015-08-13 09:56:24

标签: php mysql join

我必须进行一次mysql审讯,我必须计算一些具有相同值的单元格,按日期计算,并从左表中加入其他信息。

表1

col1 | col2     | col3
ALEX | today    | finished
JOHN | today    | finished
TIM  | today    | finished
JOHN | today    | unfinished
JOHN | today    | finished
TIM  | tommorow | finished

表2

col4 | col5
ALEX | mail1@website.tld
JOHN | mail1@website.tld
TIM  | mail1@website.tld

我试过这段代码:

sql="
SELECT col2
     , col1
     , col3
     , COUNT(*) 
  FROM table1 
  LEFT 
  JOIN table2  
    ON table1.task_cine = table2.col4 
 WHERE table1.col3='finished' 
 GROUP 
    BY col1";
$result = mysql_query($sql) or die($sql.mysql_error());
while($rows=mysql_fetch_array($result))
{
    echo "user : ";
    echo $rows['col1'];
    echo ", on date: ";
    echo $rows['col2'];
    echo " have ";
    echo $row['COUNT(*)'];
    echo " finished, and have email address";
    echo $rows['col5'];
}
你能帮帮我吗?我试图理解php,但很难。

2 个答案:

答案 0 :(得分:1)

尝试: -
mysql_fetch_assoc($结果)

而不是 mysql_fetch_array($结果)

使用带有mysql_fetch_array($ result)

的数组参数的索引

为$ rows [0],$ rows [1]

答案 1 :(得分:1)

试试这个:

$sql="
SELECT col1
     , col2
     , col5
     , COUNT(*) 
  FROM table1 
  LEFT 
  JOIN table2  
    ON table1.task_cine = table2.col4 
 WHERE table1.col3='finished' 
 GROUP 
    BY col1";
$result = mysql_query($sql) or die($sql.mysql_error());
while($rows=mysql_fetch_array($result))
{
    echo "user : ";
    echo $rows['col1'];
    echo ", on date: ";
    echo $rows['col2'];
    echo " have ";
    echo $rows['COUNT(*)'];
    echo " finished, and have email address";
    echo $rows['col5'];
}