MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 1st, 2012, 03:04 AM
BenWalters BenWalters is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 10 BenWalters User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 30 sec
Reputation Power: 0
Unique Identifier

Hi All,

I guess this is possible but I am just unaware of how to make this happen!!

Ok so I have people coming to my site and filling out a form which, when submitted gets posted to a DB (SQL)
My question is can I take for instance, the Country code (GB) & date (2012-11-01) and add an 5 digit auto incrementing number on the end. So the unique identifier will be GB2012110100001

Can this happen? If so How?

Reply With Quote
  #2  
Old November 1st, 2012, 04:03 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
Code:
DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table 
(id SMALLINT(5) NOT NULL AUTO_INCREMENT
,country_code CHAR(2) NOT NULL
,dt DATE NOT NULL
,PRIMARY KEY(country_code,dt,id)
) ENGINE = MyISAM;

INSERT INTO my_table VALUES 
(NULL,'GB',CURDATE()),
(NULL,'GB',CURDATE()),
(NULL,'DE',CURDATE()),
(NULL,'DE',CURDATE()),
(NULL,'GB',CURDATE()),
(NULL,'GB','2012-11-02');

SELECT * FROM my_table;
+----+--------------+------------+
| id | country_code | dt         |
+----+--------------+------------+
|  1 | DE           | 2012-11-01 |
|  2 | DE           | 2012-11-01 |
|  1 | GB           | 2012-11-01 |
|  2 | GB           | 2012-11-01 |
|  3 | GB           | 2012-11-01 |
|  1 | GB           | 2012-11-02 |
+----+--------------+------------+

SELECT CONCAT(country_code,DATE_FORMAT(dt,'%Y%m%d'),LPAD(id,5,0)) x FROM my_table;
+-----------------+
| x               |
+-----------------+
| DE2012110100001 |
| DE2012110100002 |
| GB2012110100001 |
| GB2012110100002 |
| GB2012110100003 |
| GB2012110200001 |
+-----------------+

Reply With Quote
  #3  
Old November 1st, 2012, 04:15 AM
BenWalters BenWalters is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 10 BenWalters User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 30 sec
Reputation Power: 0
Quote:
Originally Posted by cafelatte
Code:
DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table 
(id SMALLINT(5) NOT NULL AUTO_INCREMENT
,country_code CHAR(2) NOT NULL
,dt DATE NOT NULL
,PRIMARY KEY(country_code,dt,id)
) ENGINE = MyISAM;

INSERT INTO my_table VALUES 
(NULL,'GB',CURDATE()),
(NULL,'GB',CURDATE()),
(NULL,'DE',CURDATE()),
(NULL,'DE',CURDATE()),
(NULL,'GB',CURDATE()),
(NULL,'GB','2012-11-02');

SELECT * FROM my_table;
+----+--------------+------------+
| id | country_code | dt         |
+----+--------------+------------+
|  1 | DE           | 2012-11-01 |
|  2 | DE           | 2012-11-01 |
|  1 | GB           | 2012-11-01 |
|  2 | GB           | 2012-11-01 |
|  3 | GB           | 2012-11-01 |
|  1 | GB           | 2012-11-02 |
+----+--------------+------------+

SELECT CONCAT(country_code,DATE_FORMAT(dt,'%Y%m%d'),LPAD(id,5,0)) x FROM my_table;
+-----------------+
| x               |
+-----------------+
| DE2012110100001 |
| DE2012110100002 |
| GB2012110100001 |
| GB2012110100002 |
| GB2012110100003 |
| GB2012110200001 |
+-----------------+


Ok so my question now is, can that concatenation be saved on the same line as the form entry when posted?

Reply With Quote
  #4  
Old November 1st, 2012, 06:25 AM
BenWalters BenWalters is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 10 BenWalters User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 30 sec
Reputation Power: 0
Ok so I have put this

Code:
CONCAT(country,DATE_FORMAT(date,'%Y%m%d'),LPAD(id,5,0))


Against the value of my uid!

However the incrementing number on the end is producing 00000 everytime, help?

Reply With Quote
  #5  
Old November 1st, 2012, 06:40 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
Quote:
Originally Posted by BenWalters
Ok so my question now is, can that concatenation be saved on the same line as the form entry when posted?


I guess it could. But why would you?

UPDATE my_table SET my_column = ...

Reply With Quote
  #6  
Old November 1st, 2012, 06:42 AM
BenWalters BenWalters is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 10 BenWalters User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 30 sec
Reputation Power: 0
Quote:
Originally Posted by cafelatte
I guess it could. But why would you?

UPDATE my_table SET my_column = ...


Hi, firstly, thanks for your help
I am a novice! I don't understand what you are saying here?

Reply With Quote
  #7  
Old November 2nd, 2012, 02:25 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
Don't store data that can be easily calculated 'on-the-fly' - unless you have a good reason for doing so, in which case, use an INSERT command followed by an UPDATE command.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Unique Identifier

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap