如何在mysql中使用id设置用户名

时间:2015-07-31 08:35:25

标签: mysql

我在mysql中有一个数据库表

UIFont *font = [UIFont systemFontOfSize:16.0 weight:UIFontWeightUltraLight];
    NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"Ultra Light Label" attributes:attrsDictionary];
    [self.title setAttributedText:attrString];

如何在mysql数据库中添加新行,以便用户名为'name'+('id'* 100)

示例:

create table userstable
(
    id int not null auto_increment,
    name varchar(80) not null,
    username  varchar(80) not null,
    primary key(id)
);

3 个答案:

答案 0 :(得分:2)

您需要触发该过程。创建以下触发器

CREATE TRIGGER username_change 
BEFORE INSERT ON userstable 
FOR EACH ROW 
BEGIN
 SET NEW.username = CONCAT(NEW.name,(NEW.id*100)); 
END

OR

INSERT INTO userstable (id,name, username) VALUES (2, 'B', CONCAT(name,(id*100)));

试试这个。

答案 1 :(得分:0)

您需要在插入后编写触发器或更新字段。类似的问题提供了更多的见解:

Can you access the auto increment value in MySQL within one statement?

答案 2 :(得分:0)

正如@ArunKrish正确指出的那样,您可以使用foreach ($data as $list){ $result = "<li class='testClass'>" . $list . "</li>"; } echo $result; 更新数据作为插入的一部分。另一种选择是使用view:

TRIGGER

您也可以按原样使用查询,而不使用查看:

CREATE VIEW v1 AS 
 SELECT id,name,CONCAT(name,id*100) AS username FROM userstable;