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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:24:45+00:00 2026-05-22T14:24:45+00:00

I have a group_concat which returns all the strings from one column. The strings

  • 0

I have a group_concat which returns all the strings from one column. The strings can be variable length. How can I select the first string returned by the group_concat (where ‘first’ is defined by a group_concat ordering clause)?


Here is a simplified example. From a table of vegetable select the cheapest vegetable from each veg type.

From this table:


veg      type   price
----------------------
carrot   root   1.23
turnip   root   0.45
sprouts  bud    3.56
...

Select this:


selectedVeg   price
-------------------
turnip        0.45
sprouts       3.56
...

My clumsy attempt:

SELECT
    SUBSTRING(
        GROUP_CONCAT(veg ORDER BY price),
        1,
        LOCATE(
            ',',
            CONCAT(GROUP_CONCAT(veg order by price), ',')
        ) - 1
    ) AS selectedVeg
FROM vegTable
GROUP BY type

So for the root veg type, ‘GROUP_CONCAT’ will return ‘turnip, carrot’. Then the locate finds the first comma. And then substring returns all the characters up to this comma. So "selectedVeg" equals ‘turnip’.

I’ve added a CONCAT to ensure there is always one comma for the LOCATE to find.

This doesn’t seem very efficient as the GROUP_CONCAT has to be run twice (and it’s quite complicated in my actual problem).

Thanks.

  • 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-22T14:24:46+00:00Added an answer on May 22, 2026 at 2:24 pm

    Use a pre-query to get the smallest price per “TYPE”, then join to THAT…

    select
          v2.veg,
          v2.Price
       from
          ( select v1.type, min( v1.price ) as MinimumPrice
                from Veggies v1
                group by v1.type ) PreQuery
          join Veggies v2
             on PreQuery.Type = v2.type
             and PreQuery.MinimumPrice = v2.price
    

    This query will return ALL vegetables at the lowest price of a given type. If you want a single row, you could change the

    v2.Veg to use GROUP_CONCAT( v2.Veg… ) as SelectedVeg

    and add a GROUP BY v2.Type to the end of the query…. Your choice.

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

Sidebar

Related Questions

So I have this query that works perfectly: SELECT users.*, GROUP_CONCAT(categories.category_name) AS categories FROM
I have a user table from which I want all values, so I have
Alright, so I have a query that looks like this: SELECT `orders`.*, GROUP_CONCAT( CONCAT(
I am using mysql 5.0. I have a stored procedure which gets all the
I have the following query: SELECT wm_concat(DISTINCT NAME) as Methods FROM TPM_TRAININGPLAN JOIN TPM_DELIVERYMETHODS
I have two tables: stamps primary stamp_id, firstStampIdInSerie_id (is same for stamps from one
I have a SQL statement which is collating lots of info from several tables
I have a query which uses the GROUP_CONCAT of mysql on an integer field.
I have a query which returns the count of a status that needs some
I have a product search query which returns rows similar to this: | id

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.