jasonc Posted December 4, 2008 Share Posted December 4, 2008 i wish to list entries where one of the fields is repeated elsewhere in the DB say i want to list all the surnames of people with the name 'smith' but only if their is more than one all the single 'smiths' are not used for this. the field is as you would expect, 'surname' what query should i use to obtain these records? Quote Link to comment https://forums.phpfreaks.com/topic/135483-list-entries-by-surname-only-if-more-than-one-exists-in-db/ Share on other sites More sharing options...
divadiva Posted December 4, 2008 Share Posted December 4, 2008 so you want to search ?right? Try select * fom table where name regexp = (check with the synatx for regular expression) Quote Link to comment https://forums.phpfreaks.com/topic/135483-list-entries-by-surname-only-if-more-than-one-exists-in-db/#findComment-706204 Share on other sites More sharing options...
Mchl Posted December 4, 2008 Share Posted December 4, 2008 SELECT surname, COUNT(*) AS count FROM table GROUP BY surname HAVING count > 1 Quote Link to comment https://forums.phpfreaks.com/topic/135483-list-entries-by-surname-only-if-more-than-one-exists-in-db/#findComment-706217 Share on other sites More sharing options...
jasonc Posted December 4, 2008 Author Share Posted December 4, 2008 i use this to display the results but only get one from each 'group' not all in each group where there are doubles or repeats entries of the same surname. i also used phpmyadmin and used the query there and got the same results, just a few of them when they is like 4 or 5 in most of the groups of 'surnames' (l_name) for ($i = 0; $i < mysql_num_rows($members); $i++) { ?><br>f_name: <?=mysql_result($members, $i, "f_name");?> l_name: <?=mysql_result($members, $i, "l_name");?> <? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/135483-list-entries-by-surname-only-if-more-than-one-exists-in-db/#findComment-706241 Share on other sites More sharing options...
Mchl Posted December 4, 2008 Share Posted December 4, 2008 So you want to have all duplicates listed? SELECT ID, surname FROM table WHERE surname IN (SELECT surname FROM table GROUP BY surname HAVING COUNT(*) > 1 ) ORDER BY surname Quote Link to comment https://forums.phpfreaks.com/topic/135483-list-entries-by-surname-only-if-more-than-one-exists-in-db/#findComment-706250 Share on other sites More sharing options...
jasonc Posted December 4, 2008 Author Share Posted December 4, 2008 ok that worked thanks i wish to display then as follows to the left is the surname and to the right is the first names. surname1 firstname1 firstname2 firstname3 --------------------------- surname2 firstname1 firstname2 --------------------------- and so on... but my code does not work please help <? $members = mysql_query("SELECT * FROM `table` WHERE `l_name` IN (SELECT `l_name` FROM `table` GROUP BY `l_name` HAVING COUNT(*) > 1 ) ORDER BY `l_name`") or die(mysql_error()); for ($i = 0; $i < mysql_num_rows($members ); $i++) { ?><div align="left">Surname: <?=mysql_result($members , $i, "theip");?></div><br><? while (mysql_result($members , $i, "l_name") != mysql_result($members , $i + 1, "l_name")) { ?> <div =align="right"><?=mysql_result($members , $i, "f_name");?></div><? $i++; } ?><hr> <? $i--; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/135483-list-entries-by-surname-only-if-more-than-one-exists-in-db/#findComment-706295 Share on other sites More sharing options...
fenway Posted December 5, 2008 Share Posted December 5, 2008 Display is not a mysql issue. Quote Link to comment https://forums.phpfreaks.com/topic/135483-list-entries-by-surname-only-if-more-than-one-exists-in-db/#findComment-706473 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.