Solved

Deal with null value when using group_concat in mysql

Posted on 2010-09-15
3
484 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

752 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