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 8173031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:59:18+00:00 2026-06-06T21:59:18+00:00

Expected result: Berries: blueberry, raspberry, strawberry Citrus: grapefruit, lime Pear SQL: CREATE TABLE IF

  • 0

Expected result:

Berries: blueberry, raspberry, strawberry
Citrus: grapefruit, lime
Pear


SQL:

CREATE TABLE IF NOT EXISTS `fruits` (
  `id` varchar(8) NOT NULL,
  `group` varchar(8) NOT NULL,
  `name` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `fruits` (`id`, `group`, `name`) VALUES
('03E7', '', 'Berries'),
('0618', '03E7', 'blueberry'),
('051B', '03E7', 'raspberry'),
('02AA', '03E7', 'strawberry'),
('035F', '', 'Citrus'),
('07A5', '035F', 'grapefruit'),
('0633', '035F', 'lime'),
('05E1', '', 'Pear');

Current PHP:

<?php

header("Content-Type: text/plain");

mysql_connect("localhost", "root", "");
mysql_select_db("test");

$query = mysql_query("SELECT a.name as `group`, b.name as name FROM fruits a LEFT JOIN fruits b ON b.`group` = a.id WHERE a.`group` = ''");

if ($query) {
    while ($row = mysql_fetch_array($query)) {
        echo $row['group'] . ': ' . $row['name'] . "\n";        
    }
    mysql_free_result($query);
}

?>

Current result:

Berries: blueberry
Berries: raspberry
Berries: strawberry
Citrus: grapefruit
Citrus: lime
Pear: 
  • 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-06-06T21:59:20+00:00Added an answer on June 6, 2026 at 9:59 pm

    Thanks @ExplosionPills discovering me about MySQL GROUP_CONCAT I have updated my snippet that returns the expected result:

    <?php
    
    $query = mysql_query("SELECT a.name as `group`, GROUP_CONCAT(b.name ORDER BY b.name SEPARATOR ', ') as name FROM fruits a LEFT JOIN fruits b ON b.`group` = a.id WHERE a.`group` = '' GROUP BY a.name");
    
    if ($query) {
        while ($row = mysql_fetch_array($query)) {
            $group = $row['group'];
            $name  = $row['name'];
    
            if (isset($name)) {
                echo $group . ': ' . $name . "\n";
            } else  {
                echo $group . "\n";
            }
        }
        mysql_free_result($query);
    }
    
    ?>
    

    SQL result:

    Showing rows 0 – 2 ( 3 total, Query took 0.0004 sec).

    group   | name
    --------+--------
    Berries | blueberry, raspberry, strawberry
    Citrus  | grapefruit, lime
    Pear    | NULL
    

    PHP result:

    Berries: blueberry, raspberry, strawberry
    Citrus: grapefruit, lime
    Pear
    

    Everyone is welcome for any better implement with better improvement?

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

Sidebar

Related Questions

Inspite of putting the following meta tag I am not getting the expected result:
I wonder why the following snippet does not give the expected result? It sorts
I'm having some trouble with Excel formats: Example: Value: 13 Expected result: 13 (not
I expected to result below but actually not. Even though it worked when I
My sanity check fails because a double variable does not contain the expected result,
Can someone shed some light on this issue? The expected result does NOT appear
Could someone explain why those calls are not returning the same expected result? unsigned
Problem: The following shell script code does not produce the expected result: # MYSQL,
Steps: Run the build including unit tests. Expected result: the unit tests are executed
The example code of section 10.6, the expected result is: after several iterations, the

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.