【发布时间】:2014-11-07 17:12:02
【问题描述】:
我的当前查询正在运行,这是我目前的方法:
//loop through the defined fields & build query
foreach($searchFields as $searchField){
// if the field is set and not empty
if(isset($_POST[$searchField]) && $_POST[$searchField] != '') {
// create a new condition while escaping the value inputed by the user (SQL Injection)
$searchConditions[] = "`$searchField` LIKE '%" . $_POST[$searchField] . "%'";
}
}
//build the query
//$getExperts_sql = "SELECT * FROM $tablename";
$getExperts_sql = "SELECT *, COUNT(*) as casecount FROM $tablename";
// if there are conditions defined
if(count($searchConditions) > 0) {
// append the conditions
$getExperts_sql .= " WHERE " . implode (' AND ', $searchConditions) ." GROUP BY first,last"; // you can change to 'OR', but I opt'd to apply the filters cumulative
}
如前所述,它正在工作..(应该如此)..但我需要帮助以获得更精细/更具体的内容。
就目前而言..它-确实-仅选择不同/唯一的名称...(这是我需要的)..但我还需要添加另一条数据以“与众不同”(如果那样的话有道理)
目前.. 如果我有重复的姓名记录.. 它只返回 1.. 但如果它们有不同的状态.. 它不会考虑到这一点。
示例数据结构:
加利福尼亚州约翰·多伊
加利福尼亚州约翰·多伊
加利福尼亚州约翰·多伊
Jane Doe CA
Jane Doe CA
纽约约翰·多伊
纽约约翰·多伊
Jenny Doe WI
比利鲍勃 PA
比利鲍勃 PA
约翰·多瓦
彼得保罗佛罗里达州
彼得保罗佛罗里达州
彼得保罗佛罗里达州
彼得保罗佛罗里达州
在我的桌子上..
我希望它只返回 1 个 John Doe(针对 CA)......但也返回 John Doe NY(1 次)和 John Doe WA(1 次)......好吧,他只在那里一次..但你明白了:)
现在,我只会让 John Doe 返回 1 次..(我相信必须取表中找到的第一个)
我希望在哪里获得“3”约翰·杜斯返回..
我想我正在寻找不同的名称和状态?在一起?...但不确定如何更改当前查询以到达那里。
我不能只在名字/姓氏上使用 DISTINCT.. 我也不能只在状态上使用它..
【问题讨论】:
-
所以只需通过参数将“状态”添加到组中? (似乎正在工作)..虽然我不清楚这是否是您仅通过链接而没有上下文的建议。