Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 416077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:26:40+00:00 2026-05-12T18:26:40+00:00

I have a table that, due to the third party system we are using,

  • 0

I have a table that, due to the third party system we are using, sometimes has duplicate data. Since the model uses an EAV method there’s no way to filter this the “right” way, so I am aggregating the data into a View – I know this is a data collection problem but it’s easier for me to fix it on the display end than go through this system and potentially break existing data and forms. I need to check one of two fields to see if one or both are entered, but only pick one (otherwise the name displays twice like this: “John,John” instead of just “John”). Here’s my code for the relevant part:

group_concat(
(
  case when (`s`.`fieldid` = 2) then `s`.`data` 
  else
    case when (`s`.`fieldid` = 35) then `s`.`data` 
    else NULL end 
  end
) separator ','),_utf8'') as first_name

If both fieldid 2 and fieldid 35 are entered, I would expect this to just return the value from fieldid = 2 and not the value from fieldid = 35, since the Else clause shouldn’t execute when the original case when is true. However it’s grabbing that, and then still executing the case when inside of the else clause?

How can I fix this code to give me either fieldid = 2 OR fieldid = 35, but avoid globbing them both together which results in the name being duplicated?

EDIT

Here is the table structure:

table: subscribers_data
subscriberid (int)   fieldid (int)   data (text)    

It uses an E-A-V structure so a sample record might be:

subscriberid          fieldid         data
1                     2               John
1                     3               Smith
1                     35              John
1                     36              Smith

with fieldid 2 and 35 being the custom field “First Name” (defined in a separate table) and fieldid 3 and 36 being “Last Name”.

Here is the full view that I’m using:

select `ls`.`subscriberid` AS `id`,
left(`l`.`name`,(locate(_utf8'_',`l`.`name`) - 1)) AS `user_id`,
ifnull(group_concat((
    case when (`s`.`fieldid` = 2) then `s`.`data` 
    when (`s`.`fieldid` = 35) then `s`.`data` 
    else NULL end) separator ','),_utf8'') AS `first_name`,
ifnull(group_concat((
    case when (`s`.`fieldid` = 3) then `s`.`data` 
    when (`s`.`fieldid` = 36) then `s`.`data` 
    else NULL end) separator ','),_utf8'') AS `last_name`,
ifnull(`ls`.`emailaddress`,_utf8'') AS `email_address`,
ifnull(group_concat((
    case when (`s`.`fieldid` = 81) then `s`.`data` 
    else NULL end) separator ','),_utf8'') AS `mobile_phone`,
ifnull(group_concat((
    case when (`s`.`fieldid` = 100) then `s`.`data` 
    else NULL end) separator ','),_utf8'') AS `sms_only` 
from ((`list_subscribers` `ls` 
join `lists` `l` on((`ls`.`listid` = `l`.`listid`))) 
left join `subscribers_data` `s` on((`ls`.`subscriberid` = `s`.`subscriberid`))) 
where (left(`l`.`name`,(locate(_utf8'_',`l`.`name`) - 1)) regexp _utf8'[[:digit:]]+') 
group by `ls`.`subscriberid`,`l`.`name`,`ls`.`emailaddress`

The view is being used as the Model for a Ruby on Rails application, so I’m using some creative hacking to fake out a “user_id” that Rails expects (we name the field list.name in the Lists table using a numeric ID that our front-end Rails app generates when we add a new user, so I’m extracting just this number to make the view look like a Rails-convention database table)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T18:26:41+00:00Added an answer on May 12, 2026 at 6:26 pm

    Anything inside group_concat() doesn’t have a way to see the context in which it’s running. So, you have have two rows in a single group, one with fieldid=2 and second with fieldid=35, it will do the following:

    • processing row with fieldid=2…
      • s.fieldid = 2 is true, return s.data
    • processing row with fieldid=35…
      • s.fieldid = 2 is false, try the else part
      • s.fieldid = 35 is true, return s.data

    This explains why is “John” returned multiple times. The only way to fix it is to run a different query outside of group_concat().

    EDIT:

    Ih you really have to do it this way, use something like this instead:

    SELECT ...
       min(CASE WHEN s.fieldid IN (2,35) THEN s.data ELSE NULL END) AS first_name
    ...
    

    Alternatively you can do group_concat(DISTINCT ...) if the two values can’t be different (otherwise you would get e.g. “John,Johny”). Why do you have two values for first_name/last_name though?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 250k
  • Answers 250k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For this type of mechanism, you'd be better off using… May 13, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer My students, who learn cin and cout first, then learn… May 13, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer If you want to rewrite requests to /character/… internally to… May 13, 2026 at 9:17 am

Related Questions

I'm working on an application for one of our departments that contains medical data.
I'm working with a third party software package that is on it's own database.
We are building a new application in .net 3.5 with SQL server database. The
I'm developing a high-volume web application, where part of it is a MySQL database

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.