Reputation: 2800
I have a table called user
in my mysql database. I have an auto increment id
field and a member_id
field. and another radio button for choosing package. S for standard and P for premium
. I want to set my member id as P000001
or S000001
for the first id according to the chosen package.
For example if the first member choose P his member_id will be P000001
if the second member choose S his member_id will be S000002
if the third member choose S his member_id will be S000003
...
...
The member_id
will be exact 7 in length with P or S.
Upvotes: 1
Views: 2930
Reputation: 19882
You can simply use this query
select lpad(member_id,8,'0')
from table
where id = 1
Upvotes: 1
Reputation: 2236
How about this.
Say you have 10 people with Premium, and 8 people with Standard.
New person comes to join Standard account.
function add_user($account_type)
{
$count = query->result(SELECT COUNT(*) WHERE `member_id` LIKE '$account_type_%');
$count = $count+1;
convert count into 7 characters;
$count = $account_type.$count;
insert into database;
}
LIKE is a SQL query to match a partial result, while % acts as a wild card.
source = http://www.techonthenet.com/sql/like.php
Upvotes: 0
Reputation: 6356
Why not simply have two tables? One for S members and one for P members. You can use zero-filled ids. If it's absolutely necessary for the numbers to be unique, i.e. you don't want both S000001 and P000001 to exist, use another table to store the unique ids and reference them in your S and P tables.
Something like:
-- Table `ids`
CREATE TABLE IF NOT EXISTS `ids` (
`master_id` INT ZEROFILL NOT NULL AUTO_INCREMENT ,
`member_type` ENUM('S','P') NULL ,
PRIMARY KEY (`master_id`) )
ENGINE = InnoDB;
-- Table `s_type`
CREATE TABLE IF NOT EXISTS `s_type` (
`s_type_id` INT NOT NULL ,
`master_id` INT NULL ,
PRIMARY KEY (`s_type_id`) ,
INDEX `fk_s_type_1` (`s_type_id` ASC) ,
CONSTRAINT `fk_s_type_1`
FOREIGN KEY (`s_type_id` )
REFERENCES `ids` (`master_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table `s_type_copy1`
CREATE TABLE IF NOT EXISTS `s_type_copy1` (
`p_type_id` INT NOT NULL ,
`master_id` INT NULL ,
PRIMARY KEY (`p_type_id`) ,
INDEX `fk_s_type_copy1_1` (`p_type_id` ASC) ,
CONSTRAINT `fk_s_type_copy1_1`
FOREIGN KEY (`p_type_id` )
REFERENCES `ids` (`master_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
If you want to go real simple, just use an auto-incremented id in conjunction with an enumerated CHAR(1) for 'S' or 'P'. You just need to put them together when you need to display. The CHAR(1) column can be used to determine membership type.
If you don't plan on adding further membership types you could even go with a TINYINT as a boolean (0 = 'S', 1 = 'P').
-- Table `ids`
CREATE TABLE IF NOT EXISTS `ids` (
`member_id` INT ZEROFILL NOT NULL AUTO_INCREMENT ,
`member_type` ENUM('S','P') NULL ,
PRIMARY KEY (`member_id`) )
ENGINE = InnoDB;
...or...
-- Table `ids`
CREATE TABLE IF NOT EXISTS `ids` (
`member_id` INT ZEROFILL NOT NULL AUTO_INCREMENT ,
`member_type` TINYINT ,
PRIMARY KEY (`member_id`) )
ENGINE = InnoDB;
Upvotes: 0