作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 2 个表:tbl_customers
和 tbl_customers_notes
我在表行中显示我的所有客户(就像一个人所做的那样),我想添加一列用于显示相关 tbl_customers_notes< 中最后已知的
表。datetime
记录
显然这是一对多关系,其中匹配的 recID 将是 customerid
。
这是我目前所拥有的:
<?php
$result = mysql_query("SELECT `customername` FROM `tbl_customers` ");
while($row = mysql_fetch_array( $result )) {
?>
<tr>
<td><?php echo $customername;?></td>
<td><?php echo 'note datetime'; ?></td>
</tr>
<? } ?>
如果我对 notes 表进行 JOIN,我会得到重复项。我可以将 tbl_customers_notes
限制为只选择该 customerid 的最后一条已知记录吗?
最佳答案
试试这个:
select c.customerid, c.customername, max(cn.note_date)
from tbl_customers c
left join tbl_customers_notes cn on c.customerid = cn.customerid
group by c.customerid, c.customername
同时按 customerid 和 customername 分组的原因是,以防相同的 customername 可能有不同的 customerid。外部连接是为了包括没有注释的客户。
关于php - MySql Select查询(限选JOIN),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7195469/
我是一名优秀的程序员,十分优秀!