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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:37:56+00:00 2026-05-27T22:37:56+00:00

I got a problem. I have data in a table that look like that

  • 0

I got a problem. I have data in a table that look like that :

route_id    route_short_name    route_long_name

AOUT1112E   12                  Direction Pont-Viau
AOUT1112O   12                  Direction Métro Cartier
JANV1212E   12                  Direction Pont-Viau
JANV1212O   12                  Direction Métro Cartier
JRAN1212E   12                  Direction Pont-Viau
JRAN1212O   12                  Direction Métro Cartier

I need the result to be like that :

12 – Direction Pont-Viau / Direction Métro Cartier

OR

12 – Direction Pont-Viau / Métro Cartier

I tought I may use two arrays but I’d like to do this in MySQL. Also, I got other routes in the table like route 13, 15, etc so it need to work in a SELECT ALL Anyone have any idea ?

Thanks a lot

  • 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-27T22:37:57+00:00Added an answer on May 27, 2026 at 10:37 pm

    To combine two strings, use CONCAT(). To join the values in a group, use GROUP_CONCAT.

    SELECT 
        *, 
        CONCAT(
            route_short_name,
            " - ",
            GROUP_CONCAT(route_long_name SEPARATOR ' / ')
        ) AS route_name
    FROM 
        ...
    GROUP BY
        route_short_name
    

    It would return something like this:

    SHORTNAME - LONGNAME1 / LONGNAME2 / LONGNAME3
    

    The above would only return one row per route_short_name. To get them all, use a subquery:

    SELECT *, (
        SELECT CONCAT(
            route_short_name, 
            " - ", 
            GROUP_CONCAT(route_long_name.b SEPARATOR " / "))
        FROM 
            your_table_name AS your_table_name_inner
        WHERE 
            your_table_name_inner.route_short_name = your_table_name_outer.short_name
        GROUP BY 
            your_table_name.route_short_name
    ) AS route_name
    FROM
        your_table_name AS your_table_name_outer
    

    The subquery is pretty much the same as the first example, except that it always returns a single row in the format SHORTNAME - LONGNAME1 / LONGNAME2 / etc. The outer query returns all records and for every record, the subquery is executed. It goes without saying that this is a heavier query than the first example.


    On closer inspection, its seems that the route_short_name is actually quite irrelevant to the database structure, unlike what I assumed.

    I’ve left the above answer, because it might help someone else.

    If I understand correctly, you want to join two (not more, not less) records, where one has a route_id ending with an E and one with an O.

    Assuming the following:

    • The ID is always 8 characters + an E or O.
    • There are always two records that belong together.

    In that case:

    SELECT 
        *, 
        CONCAT(a.route_short_name, ' - ', a.route_long_name, ' / ', b.route_long_name)
    FROM
        your_table_name AS a
    JOIN
        your_table_name AS b
    ON
        LEFT(a.route_id, 8) = LEFT(b.route_id, 8)
    AND
        a.route_id != b.route_id
    

    This does the following:

    1. Return the short name, a dash, long name 1, slash, long name 2.
    2. Join based on the first 8 characters of the IDs (LEFT(str, n) returns the first n characters of a string).
    3. The IDs may not be an exact match (which prevents joining with itself).

    This might be rather slow (due to the use of LEFT()), but MySQL might have some black magic that optimizes things and indexes might help as well. Benchmark it on your real table.

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

Sidebar

Related Questions

I got this problem. I have a form that retrieves a table data using
Got a problem with a query I'm trying to write. I have a table
I have got a performance problem about TextField.htmlText +=msg .And I know that TextField.appendText(msg)
I have a table data like this: <table class=tablesorter id=dea> <thead> <tr> <th class=header
I have an assignment problem that i can't manage to fix and got totally
It seems this problem is quite popular on programming. I have a table that
I've got a table of crime data. In a simplified version, it would look
I've got a problem where I have a .co.uk domain of which I am
I've got the following problem: I have two tables: (simplified) +--------+ +-----------+ | User
I have got a problem with calling a global function, which takes a pointer

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.