Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Prefixing and auto-increment field (0811/1234)
Old 11-16-2008, 04:27 PM Prefixing and auto-increment field (0811/1234)
Experienced Talker

Posts: 39
Trades: 0
Hello

I have a web application (php/mySQL) that has an auto-increment primary key field called job_id.

Could anyone help me add a prefix to this?

I want to add the year and month (YYMM/) in front of the field (e.g. 0811/1234 then 0811/1235 – assuming current month and year is Nov 2008).

Maybe I’m wrong about adding the prefix to the primary key. Should I maybe add an additional field to maintain this?

Any advice greatly appreciated...

Regards

Paul

Last edited by 69me; 11-16-2008 at 04:28 PM..
69me is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-16-2008, 04:43 PM Re: Prefixing and auto-increment field (0811/1234)
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Use a separate field
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-16-2008, 04:47 PM Re: Prefixing and auto-increment field (0811/1234)
Experienced Talker

Posts: 39
Trades: 0
Cheers Chris!

Next question, on insert the original job_id is given the next auto-increment id (e.g. "1776"), how do I append this "1776" to my "0811/" prefix to make the new job_ref field equal "0811/1776"

Thanks again!
69me is offline
Reply With Quote
View Public Profile
 
Old 11-16-2008, 04:57 PM Re: Prefixing and auto-increment field (0811/1234)
Experienced Talker

Posts: 41
Name: Jabis Sevon
Location: Tampere, Finland
Trades: 0
http://www.php.net/mysql_insert_id would be a good place to start
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
jabis is offline
Reply With Quote
View Public Profile Visit jabis's homepage!
 
Old 11-16-2008, 05:31 PM Re: Prefixing and auto-increment field (0811/1234)
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Why would you do that on insert? It would be wasting space in the database by duplicating data and making the job_ref field need to be a larger field type.

as YYMM it can be a CHAR(4) or VARCHAR(4), but by appending the auto_increment it would need to be VARCHAR(make_a_guess) because you do not know how many records will be in the table.

Code:
SELECT CONCAT_WS("/", y_m_column, CAST(job_id AS CHAR)) AS refid FROM table
will append the YM column to the id with "/" as a separator as the records are retrieved.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-16-2008, 05:52 PM Re: Prefixing and auto-increment field (0811/1234)
Experienced Talker

Posts: 39
Trades: 0
great stuff guys!! its nearly 11pm here in Scotland so I'll try some of this out tomorrow morning and let you know how I get on!

thanks again!!!
69me is offline
Reply With Quote
View Public Profile
 
Old 11-17-2008, 04:58 PM Re: Prefixing and auto-increment field (0811/1234)
Experienced Talker

Posts: 39
Trades: 0
Quote:
Originally Posted by chrishirst View Post

Code:
SELECT CONCAT_WS("/", y_m_column, CAST(job_id AS CHAR)) AS refid FROM table
Works great in isolation but not within my Adobe Dreamweaver Develop Toolbox (ADDT) web application.

Totally GUTTED! Need to find a solution ASAP.

Any other ideas???

Thanks for you time Chris...
69me is offline
Reply With Quote
View Public Profile
 
Old 11-17-2008, 06:05 PM Re: Prefixing and auto-increment field (0811/1234)
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
but not within my Adobe Dreamweaver Develop Toolbox (ADDT) web application.
Set up the queries outside of that, otherwise you'll be stuck with the most inefficient ways of querying the database imaginable.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-18-2008, 06:08 AM Re: Prefixing and auto-increment field (0811/1234)
Experienced Talker

Posts: 39
Trades: 0
Chris, thanks again!

I did find two other points of note...

1) I found the separator has to be in '/' as opposed to "/"
PHP Code:
SELECT CONCAT_WS("/"y_m_columnCAST(job_id AS CHAR)) AS refid FROM table 
PHP Code:
SELECT CONCAT_WS('/'y_m_columnCAST(job_id AS CHAR)) AS refid FROM table 
2) I find that mySQL does not allow you to use an alias field in a WHERE clause. Do you know of any further work around I could use to allow me to progress as I need to filter by this concat field alias???

Cheers

Paul
69me is offline
Reply With Quote
View Public Profile
 
Old 11-18-2008, 06:48 AM Re: Prefixing and auto-increment field (0811/1234)
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Why would you filter by the alias?
the auto increment column is going to be unique anyway
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Prefixing and auto-increment field (0811/1234)
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.41716 seconds with 12 queries