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

  • SEARCH
  • Home
  • 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 80375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:17:34+00:00 2026-05-10T21:17:34+00:00

I’m trying to reorder/group a set of results using SQL. I have a few

  • 0

I’m trying to reorder/group a set of results using SQL. I have a few fields (which for the example have been renamed to something a bit less specific), and each logical group of records has a field which remains constant – the address field. There are also fields which are present for each address, these are the same for every address.

id  forename    surname     address 1   John        These       Address1 2   Lucy        Values      Address1 3   Jenny       Are         Address1 4   John        All         Address2 5   Lucy        Totally     Address2 6   Jenny       Different   Address2 7   Steve       And         Address2 8   Richard     Blah        Address2  address     John    Lucy            Jenny       Steve       Richard Address1        These   Values          Are         (null)      (null) Address2        All     Totally         Different   And         Blah 

For example: John,Lucy,Jenny,Steve and Richard are the only possible names at each address. I know this because it’s stored in another location.

Can I select values from the actual records in the left hand image, and return them as a result set like the one on the right? I’m using MySQL if that makes a difference.

  • 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. 2026-05-10T21:17:34+00:00Added an answer on May 10, 2026 at 9:17 pm

    Assuming that the column headings ‘john’, ‘lucy’ etc are fixed, you can group by the address field and use if() functions combined with aggregate operators to get your results:

    select max(if(forename='john',surname,null)) as john,        max(if(forename='lucy',surname,null)) as lucy,        max(if(forename='jenny',surname,null)) as jenny,               max(if(forename='steve',surname,null)) as steve,               max(if(forename='richard',surname,null)) as richard,        address from tablename  group by address; 

    It is a bit brittle though.

    There is also the group_concat function that can be used (within limits) to do something similar, but it will be ordered row-wise rather than column-wise as you appear to require.

    eg.

    select address, group_concat( concat( forename, surname ) ) tenants  from tablename group by address; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to select an H1 element which is the second-child in its group
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have an array which has BIG numbers and small numbers in it. I
Basically, what I'm trying to create is a page of div tags, each has

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.