I need to create a custom query using specific characters from database

Discussion in 'MySQL' started by scottjgd, Sep 12, 2010.

  1. #1
    What i have is a rather large database of flight numbers based on divisions. the flight number is entered as following eg: CCC-NOC001 where the number breaks down as follows CCC airline Code NOC division Code and 001 actual flight number.

    Now i am trying to create a filter that will select flight number from the schedule based on the division code. now to clarify the way the flight numbers are entered into the database is in 2 fields the first being code which is always entered as CCC and the flight number which would be NOC001 etc.

    So what i need if it is possible is to strip the numbers from the end of the flight number so that all that is searched for is the NOC or one of the other division codes currently there are 9 separate divisions. And i would like to be able to filter the queries so that if from the drop down list they select NOC then only the flight numbers with NOC will show in the table.

    CCC-NOC001
    CCC-NOC002
    ....

    Any help you can give me would be greatly appreciated
     
    scottjgd, Sep 12, 2010 IP
  2. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Hi, just a suggestion: why didn't you use different mysql columns for all parts-the final result will be simply concat and you'll not have any troubles.

    MySQL has not preg_replace...
    Regards :)
     
    koko5, Sep 13, 2010 IP
  3. scottjgd

    scottjgd Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply but that would mean rewriting about 15 different scripts to work with that change. But i think you may have put me in the right direction though. I was reading on the page you linked and came across LEFT and LTRIM would those work for my situation? If so how would i code those into my query.
    This is the block of code that needs to be altered to make the function i need work. I kinda figure using the LTRIM to remove the - from the beginning of the flight number, and the LEFT to select the first 3 characters in the str. But I am not sure how to incorporate that into the code.
    /**
         * Get all of the flight divisions
         */
        public static function getAllFlightNum()
        {
            $key = 'all_flight';
            $all_flight = CodonCache::read($key);
            
            if($all_flight === false)
            {
                $sql = 'SELECT * FROM ' . TABLE_PREFIX .'schedules 
                        ORDER BY `flightnum` ASC';
                        
                    
                $all_flight= DB::get_results($sql);
                
                CodonCache::write($key, $all_flight, 'long');
            }
            
            return $all_flight;
        }
    PHP:
    right now it returns all flights in the database
     
    Last edited: Sep 13, 2010
    scottjgd, Sep 13, 2010 IP
  4. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #4
    Just pass $all_flight in php preg_replace before function returns-better than mysql functions, because I'm not sure the length is and will be (in the future-when you add new data in DB) always constant length.
     
    koko5, Sep 13, 2010 IP
  5. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #5
    OK, here it's in MySQL:

    
    DROP TABLE IF EXISTS TESTING;
    
    CREATE TABLE TESTING(S VARCHAR(255));
    INSERT INTO TESTING VALUES(
    'CCC-NOC001'),
    ('CCC-NOC002');
    
    SELECT SUBSTRING_INDEX(S,'-',1) AS CCC,SUBSTRING_INDEX(S,'-',-1) AS NOC FROM TESTING;
    
    Code (markup):
    :)
    Edit: Please, check ur PMB-sent you another example!
     
    Last edited: Sep 13, 2010
    koko5, Sep 13, 2010 IP