Tuesday, 15 July 2014

php - MySQL: How to get distinct name but also by state? -



php - MySQL: How to get distinct name but also by state? -

my currect query working, here current approach:

//loop through defined fields & build query foreach($searchfields $searchfield){ // if field set , not empty if(isset($_post[$searchfield]) && $_post[$searchfield] != '') { // create new status while escaping value inputed user (sql injection) $searchconditions[] = "`$searchfield` '%" . $_post[$searchfield] . "%'"; } } //build query //$getexperts_sql = "select * $tablename"; $getexperts_sql = "select *, count(*) casecount $tablename"; // if there conditions defined if(count($searchconditions) > 0) { // append conditions $getexperts_sql .= " " . implode (' , ', $searchconditions) ." grouping first,last"; // can alter 'or', opt'd apply filters cumulative }

as mentioned working.. (as should).. need help getting little more granular/specfic.

as stands.. -does- select distinct/unique names... (which need).. need add together in piece of info 'distinct' (if makes sense)

currently.. if have duplciate names records.. returns 1.. if have different state.. doesnt take account.

example info structure:

john doe ca

john doe ca

john doe ca

jane doe ca

jane doe ca

john doe ny

john doe ny

jenny doe wi

billy bob pa

billy bob pa

john doe wa

peter paul fl

peter paul fl

peter paul fl

peter paul fl

in table..

i want homecoming 1 john doe (for ca)... homecoming john doe ny (1 time), john doe wa (1 time)... hes in there 1 time.. thought :)

right now, john doe returned 1 time.. (must take first 1 found in table believe)

where looking '3' john returned..

i guess looking ta distinct name & state? together?... not sure how can alter current query there.

i cant utilize distinct on first/last name.. , cant utilize on state either..

if state in table, work. add together state group by statement.

$getexperts_sql .= " " . implode (' , ', $searchconditions) ." grouping first,last, state"; // can alter 'or', opt'd apply filters cumulative

php mysql distinct

No comments:

Post a Comment