gpt4 book ai didi

php - mySQL 查询,对最新记录而非所有记录进行 JOIN

转载 作者:行者123 更新时间:2023-11-29 06:06:39 26 4
gpt4 key购买 nike

以下代码用于在查询中获取记录。它使用 electors.ID 从第二个表中查找相应的 vote_intention.elector。

$criteria = "FROM voting_intention,electors WHERE voting_intention.elector = electors.ID AND voting_intention.pledge IN ('C','P') AND electors.postal_vote = 1 AND electors.telephone > 0"

问题是一些选举人在 vote_intentions 表中会有多个 promise 。

我需要它仅根据每个选举者的字段 votin_intention.date 匹配最新的 voting_intention.pledge

实现这一点的最简单方法是什么。

其余代码:

function get_elector_phone($criteria){
$the_elector = mysql_query("SELECT * $criteria ORDER BY electors.ID ASC"); while($row = mysql_fetch_array($the_elector)) {
echo $row['ID'].','; }
}

最佳答案

您可以使用 MAX() 函数进行子选择。将以下内容添加到您的 WHERE 子句中。

AND voting_intention.date = (select MAX(date) 
from voting_intention vote2
where voting_intention.elector = vote2.elector)

这是一个SQLFiddle of the results.

关于php - mySQL 查询,对最新记录而非所有记录进行 JOIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11249178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com