Solved

Deal with null value when using group_concat in mysql

Posted on 2010-09-15
3
483 Views
Last Modified: 2012-05-10
I have the following query in mysql views:

select distinct `tblApplications`.`fldApplicationID` AS `fldApplicationID`,`tblLotonPlan`.`fldLotonPlanID` AS `fldLotonPlanID`,
group_concat(_latin1' ',`tblEntity`.`fldName`,' ',`tblEntity`.`fldSurname`,_latin1' ' separator ',') AS `Owners`
from (((`tbl_Lnk_Applications`
join `tblLotonPlan` on`tbl_Lnk_Applications`.`fldApplicationLinkID` = `tblLotonPlan`.`fldApplicationLinkID`)
join `tblApplications` on`tblApplications`.`fldApplicationID` = `tbl_Lnk_Applications`.`fldApplicationID`)
join `tblLandowner` on `tblLandowner`.`fldLotonPlanID` = `tblLotonPlan`.`fldLotonPlanID`)
join `tblEntity` on`tblEntity`.`fldEntityID` = `tblLandowner`.`fldEntityID`
group by `tblLotonPlan`.`fldLotonPlanID`

The main issue is the field

group_concat(_latin1' ',`tblEntity`.`fldName`,' ',`tblEntity`.`fldSurname`,_latin1' ' separator ',') AS `Owners


This fields return a null value when `tblEntity`.`fldSurname` is null such as in the case of a business name.


I have tried using


group_concat(_latin1' ',`tblEntity`.`fldName`,' ',IFNULL(`tblEntity`.`fldSurname`,""),_latin1' ' separator ',') AS `Owners

Problem with this approach is that it repeats an instant of the name for each space in the name. For example:
Expert Exchange Forum will return
Expert Exchange Forum,Expert Exchange Forum,Expert Exchange Forum

How do I deal with this

0
Comment
Question by:Sheils
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
DalHorinek earned 500 total points
ID: 33680353
The problem is that group_concat just takes all occurences and concats them.

Try to add DISTINCT

group_concat(DISTINCT _latin1' ',`tblEntity`.`fldName`,' ',`tblEntity`.`fldSurname`,_latin1' ' separator ',') AS `Owners

Open in new window

0
 
LVL 16

Author Comment

by:Sheils
ID: 33680442
Hi Dal

I will try this out at work tomorrow and get back to you.

0
 
LVL 16

Author Closing Comment

by:Sheils
ID: 33699186
yes that did the trick. Thanks Mate
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question