Jump to content

5 column name row not returning all results


richrock

Recommended Posts

Hi,

 

I've got a 5 column name table, and need to search it.  Simple in theory, but it's not returning all the names.

 

For example, if I have the name 'John Andrew Smith', 'John Smith', 'John Edward Smith'  and search for Smith, it won't return John Andrew Smith...

 

It's part of a search system which calls a script to search based on what is typed:  Here's the mysql query:

 

$searchq		=	strtoupper($_GET['q']);
//$getRecord_sql	=	'SELECT * FROM '.$SQL_FROM.' WHERE '.$SQL_WHERE.' LIKE "'.$searchq.'%" GROUP BY surname LIMIT 30';
$getRecord_sql	= "SELECT surname, firstname, secondname, thirdname, fourthname, fifthname FROM ".$SQL_FROM." WHERE ";
    $getRecord_sql .= "`surname` LIKE '".$searchq."%' OR";
    $getRecord_sql .= " `firstname` LIKE '".$searchq."%' OR";
    $getRecord_sql .= " `secondname` LIKE '".$searchq."%' OR";
    $getRecord_sql .= " `thirdname` LIKE '".$searchq."%' OR";
    $getRecord_sql .= " `fourthname` LIKE '".$searchq."%' OR";
    $getRecord_sql .= " `fifthname` LIKE '".$searchq."%'";
//$getRecord_sql .= " ORDER BY surname ASC";
    $getRecord_sql .= " GROUP BY firstname ORDER BY surname LIMIT 30";
//$getRecord_sql .= " LIMIT 15";

 

I've set the LIKEs to wildcard the one side, so it's quite speedy, but can't figure why it doesn't show all results.  Not sure if it's something to do with being distinct on the surname, or the grouping needs changing.  Any ideas appreciated.  Thanks

Link to comment
Share on other sites

Because your grouping clause only specifies firstname then duplicates will be removed from the results where the firstname matches. I would have thought you would not need group by, unless you specify all the fields.

 

The group by clause is explained http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

 

Are you expecting duplicates across all the name fields?

Link to comment
Share on other sites

Thanks for the reply.

 

I have multiple entries for each name, but only need to list it once... I tried removing GROUP BY - it wasn't right... I'll see if I can extract some examples from the DB (don't worry, they're all dead musicians, so no privacy issues there AFAIK  ::) )

 

If I group by surname, it seems to miss out differing sets of names, it's really hard to explain....

Link to comment
Share on other sites

What I mean is :

 

John Smith 1858-1890 Vienna

John Andrew Smith 1870-1932 Paris

John Robert Smith 1792-1826 Dusseldorf

John Andrew Smith 1870-1932 Paris

John Robert Smith 1792-1826 Dusseldorf

John Andrew Smith 1870-1932 Paris

John Robert Smith 1792-1826 Dusseldorf

John Smith 1858-1890 Vienna

 

So there are duplicate entries for the same name in the same table - maybe multiple was the wrong word... :(

 

So if I were to search for  "John Robert" - I should get 1 result, if I search for "John", I should get 3 results.  Likewise, if I search for "Robert", I should also get 1 result.

Link to comment
Share on other sites

Hi

 

Think a version of you SQL like this would give you what you want:-

 

SELECT surname, firstname, secondname, thirdname, fourthname, fifthname, concat_ws(" ",ifnull(surname,'') , ifnull(firstname,'') , ifnull(secondname,'') , ifnull(thirdname,'') , ifnull(fourthname,'') , ifnull(fifthname,'') ) ConcatName 
FROM namestable 
WHERE 
`surname` LIKE 'john%' OR
`firstname` LIKE 'john%' OR
`secondname` LIKE 'john%' OR
`thirdname` LIKE 'john%' OR
`fourthname` LIKE 'john%' OR
`fifthname` LIKE 'john%'
GROUP BY ConcatName 
ORDER BY surname 

 

This is a misuse of group by, and you probably should use a distinct instead:-

 

SELECT distinct surname, firstname, secondname, thirdname, fourthname, fifthname
FROM namestable 
WHERE 
`surname` LIKE 'john%' OR
`firstname` LIKE 'john%' OR
`secondname` LIKE 'john%' OR
`thirdname` LIKE 'john%' OR
`fourthname` LIKE 'john%' OR
`fifthname` LIKE 'john%'
ORDER BY surname 

 

All the best

 

Keith

Link to comment
Share on other sites

So there are duplicate entries for the same name in the same table - maybe multiple was the wrong word... :(

 

And you're sure that these are actually the same record in the table? that seems impossible....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.