Php mysql选择、计数、左连接和显示
我必须进行一次mysql查询,我必须按日期计算一些具有相同值的单元格,然后从另一个表中加入其他信息 表1Php mysql选择、计数、左连接和显示,php,mysql,join,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 |
col1 | col2 | col3
ALEX | today | finished
JOHN | today | finished
TIM | today | finished
JOHN | today | unfinished
JOHN | today | finished
TIM | tommorow | finished
表2col4 | 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,但很难理解。试试:-mysql_fetch_assoc($result) 而不是 mysql\u fetch\u数组($result) 或 将索引与数组参数一起用于mysql\u fetch\u数组($result) 作为$rows[0],$rows[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'];
}
给COUNT(*)一个别名,比如
total
——看看PHP的现代API(PDO或mysqli)。此外,很好的做法是限定列来自哪个表-因此table1.col3
,等等。您确定提到的列task\u cine
属于table1
?在SQL中,位于table1.col1=table2.col4,对不起。但是仍然没有显示$rows['col5']您忘了将$
符号添加到变量sql
。只显示col1 end other text,没有其他内容(其他$rows[])。是的,在代码中我有$row,而不是$rows,并且我进行了修改。它可以工作,但是$rows['col5']仍然不工作。$rows['col5']
不工作,因为您没有在SQL查询中选择它。您还应该使用echo$rows['COUNT(*')代码>而不是echo$row['COUNT(*')因为您的数据集是$rows
。