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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:56:20+00:00 2026-05-17T18:56:20+00:00

I am using mysql & perl. I have the following values for one of

  • 0

I am using mysql & perl. I have the following values for one of my columns in my database:

123 Hollywood
345 New York
847 Hollywood
192 Boston
876 Chicago

I want to select all of the distinct cities….ie, the results would be:

Hollywood
New York
Boston
Chicago

Is there a way to do this via msyql? I would imagine some sort of REGEXP but can’t seem to get the logic:

SELECT * FROM table WHERE address REGEXP ''
  • 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-17T18:56:21+00:00Added an answer on May 17, 2026 at 6:56 pm

    Assuming that your address is always in the format <numbers><space><city>

    SELECT
      DISTINCT TRIM(LEADING SUBSTRING_INDEX(address, ' ', 1) FROM address) AS city
    FROM
      locations
    

    Here’s the test data I used to verify the query works. Note that I named the test table locations. My MySQL server is version 5.1.36:

    --
    -- Table structure for table `locations`
    --
    
    CREATE TABLE IF NOT EXISTS `locations` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `address` varchar(255) COLLATE utf8_bin DEFAULT NULL,
      PRIMARY KEY (`id`),
      KEY `address` (`address`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=6 ;
    
    --
    -- Dumping data for table `locations`
    --
    
    INSERT INTO `locations` (`id`, `address`) VALUES
    (1, '123 Hollywood'),
    (2, '345 New York'),
    (3, '847 Hollywood'),
    (4, '192 Boston'),
    (5, '876 Chicago');
    

    Here’s the exact query I ran on that table:

    SELECT DISTINCT TRIM(
    LEADING SUBSTRING_INDEX( address, ' ', 1 )
    FROM address ) AS city
    FROM locations
    

    Here’s the result I got:

    **city**
    Hollywood
    Boston
    New York
    Chicago
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming you were modeling a Q&A database using MySQL, I am aware of two
I've been using PHP & MySQL for ages and am about to start using
Using MySQL syntax, how would I write a query to return the following (I
Note: Using MySQL 4.0, which means no subqueries (at present). I have 2 tables:
I am using MySQL and PHP for a project I am working. I have
Using MySQL & Java Script I want to make a mysql connection in Java
Using MySQL & SQL Server ID sDate 001 03/06/2010 002 07/08/2010 .... .... sDate
I'm having trouble deleting images from a mysql database using php. The addition of
I am restoring a mysql database with perl on a remote server with about
Have some Perl code which is using the DBI module - (the code is

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.