I have a text file that I parse each month and insert the data into my database. The text file looks like this.
------------------------------------------------------------------------------------------------------------------------------------------------------------
BLK. NO 410 | 1 2| 3 4 5 6 7 | 8 9| 10 11 12 13 14 |15 16| 17 18 19 20 21 |22 23| 24 25 26 27 28 |29 30| 31 1 2
BLK 88.35 |SA SU| MO TU WE TH FR |SA SU| MO TU WE TH FR |SA SU| MO TU WE TH FR |SA SU| MO TU WE TH FR |SA SU| MO TU WE
CR. 88.35 |194 / 159 | |194 / 159 | |194 / 159 | |194 / 159 | |239
OFF 14 : : VER DFW TRC DFW X : : VER DFW TRC DFW X : : VER DFW TRC DFW X : : VER DFW TRC DFW X : : CUU DFW
C/O 06.00 TAFB 216.15 194=/1410/1055/0740, 159=/0900/1700/1400, 239=/1845/1945/0155
EM3 25.55 ERD 42.00 EM4 20.40
-----------------------------------------------------------------------------------------------------------------------------
This text file has from 100 to 300 of these records (we refer to it as a “line of flying”) and I use it to determine my flight schedule each month. I am parsing this file just fine but the company added a new wrinkle.
This “line” can be for a pilot or a flight attendant. Until recently each line could be for a captain or First Officer. But now some files have captain lines and first officer lines. The flight attendant file could have 1 or two flight attendant positions or more depending on the type of aircraft flown.
I need to create a new column in my database that reflects this new information. Once the lines are saved to the database, they are only searched, never updated again. I don’t want to add a new table just to keep track of this information. I want to add a new column to the existing table. Here is my strategy.
When I parse the text file, I will keep track of the positions available. If it is for Captain, I will save “CA” to the database. If it is first officer, I will save “FO”. If it is for both a captain and First officer, I will save “CAFO”. For flight attendants I will save a number “1” for 1 flight attendant poistion. For each additional poistion, I will just increment it one number. This would allow for expansion later if larger aiplanes required more flight attendant poistions.
So to recap, I will save one the following for each database record.
- “CAFO”
- “CA”
- “FO”
- “1”
- “2”
Now I will need to access the database and retrieve these records for my web program. If a captain wants to retreive records He will want only captain lines which would be “CAFO” or “CA”. And just the opposite for first officers.
When I do the query, I found that I could use the mySQL command SUBSTRING to just grab the first 2 or last 2 characters.
for captain lines
SELECT
Line_number,html_textFROMlinesWHERE SUBSTRING(Crew_type,1,2)=”CA”
This would just get the first two beginning characters from the record and it would match both cases.
For First officers I start at the end of the string and grab last two characters.
SELECT
Line_number,html_textFROMlinesWHERE SUBSTRING(Crew_type,-2)=”FO”
For flight attendants I could compare the number.
If a flight attendant only wanted lines with 1 position.
SELECT
Line_number,html_textFROMlinesWHERE Crew_type = “1”
Or if they want all possible flight attendant positions
SELECT
Line_number,html_textFROMlinesWHERE Crew_type >= “1”
I would set the column type to ENUM with the values I listed above.
My question is there a better way to approach this problem? I will only be inserting once, but will be retreiving a lot. Will the SUBSTRING command slow down the queries too much?
will be a performance killer, as it calculates the substring for all records, not only those used. You want
Ditto for FO.
So somewhere in your PHP iw would define
and then use
with $crew_type being one of ‘CA’,’FO’,’FA’