找到 4219 篇文章 适用于 MySQLi
596 次查看
要设置 MySQL 表的自增初始值,请使用 ALTER 命令。第一步是:alter table yourTableName modify yourColumnName int NOT NULL AUTO_INCREMENT PRIMARY KEY, add index(yourColumnName);第二步如下:alter table yourTableName AUTO_INCREMENT=yourStartingValue;为了理解以上语法,让我们创建一个表。创建表的查询如下:mysql> create table setAutoIncrementDemo -> ( -> UserId int, -> UserName varchar(20) -> ); Query OK, 0 rows affected (0.75 sec)现在实施以上两个步骤来设置 MySQL 表的自增初始值。步骤 1 -查询如下:mysql> alter ... 阅读更多
646 次查看
让我们看一个例子,并首先创建一个表。mysql> create table Add1ToExistingValue -> ( -> Value int -> ); Query OK, 0 rows affected (0.56 sec)使用 insert 命令在表中插入一些记录。查询如下:mysql> insert into Add1ToExistingValue values(10); Query OK, 1 row affected (0.12 sec) mysql> insert into Add1ToExistingValue values(13); Query OK, 1 row affected (0.15 sec) mysql> insert into Add1ToExistingValue values(15); Query OK, 1 row affected (0.13 sec) mysql> insert into Add1ToExistingValue values(16); Query OK, 1 row affected (0.14 sec) mysql> insert into Add1ToExistingValue values(20); Query OK, 1 row affected (0.16 sec) mysql> insert into Add1ToExistingValue values(40); Query OK, 1 row affected (0.15 sec) mysql> insert into Add1ToExistingValue values(50); Query OK, 1 row affected (0.11 sec) mysql> insert into Add1ToExistingValue values(55); Query OK, 1 row affected (0.17 sec) mysql> insert into Add1ToExistingValue values(56); Query OK, 1 row affected (0.17 sec)使用 select 语句显示表中的所有记录。查询如下:mysql> select *from Add1ToExistingValue;输出如下+-------+ | Value | +-------+ | 10 ... 阅读更多
698 次查看
当您在目标表中插入记录时,如果列数与值数不匹配,就会发生此类错误。为了进行演示,让我们创建一个表:mysql> create table errorDemo -> ( -> User_Id int NOT NULL AUTO_INCREMENT, -> User_Name varchar(20), -> PRIMARY KEY(User_Id) -> ); Query OK, 0 rows affected (0.47 sec)错误如下:mysql> insert into errorDemo values('John'); ERROR 1136 (21S01): Column count doesn't match value count at row 1要避免此类错误,您需要使用以下语法:insert into yourTableName(yourColumnName1, yourColumnName2, ...N)values(yourValue1, yourValue2, ....N);插入一些 ... 阅读更多
169 次查看
要将行恢复到默认列值,让我们首先创建一个演示表:mysql> create table defaultDemo -> ( -> Id int -> ); Query OK, 0 rows affected (0.48 sec)使用 insert 命令在表中插入一些记录。查询如下:mysql> insert into defaultDemo values(10); Query OK, 1 row affected (0.25 sec) mysql> insert into defaultDemo values(20); Query OK, 1 row affected (0.13 sec) mysql> insert into defaultDemo values(30); Query OK, 1 row affected (0.14 sec) mysql> insert into defaultDemo values(40); Query OK, 1 row affected (0.11 sec) mysql> insert into defaultDemo values(80); Query OK, ... 阅读更多
1K+ 次查看
让我们首先创建一个表:mysql> create table recordsDemo -> ( -> UserId int, -> Value int -> ); Query OK, 0 rows affected (0.52 sec)现在使用 insert 命令在表中插入一些记录。查询如下:mysql> insert into recordsDemo values(1, 10); Query OK, 1 row affected (0.17 sec) mysql> insert into recordsDemo values(3, 598); Query OK, 1 row affected (0.18 sec) mysql> insert into recordsDemo values(5, 786); Query OK, 1 row affected (0.25 sec) mysql> insert into recordsDemo values(7, 189); Query OK, 1 row affected (0.16 sec) mysql> insert into recordsDemo values(9, 345); ... 阅读更多
200 次查看
让我们看看如何在 MySQL 中循环遍历存储过程:mysql> DELIMITER // mysql> CREATE PROCEDURE do_WhileDemo(LastValue INT) -> BEGIN -> SET @loop = 0; -> REPEAT -> SET @loop= @loop+ 1; -> select @loop; -> UNTIL @loop >LastValue -> END REPEAT; -> END // Query OK, 0 rows affected (0.17 sec) mysql> DELIMITER ;现在使用 CALL 命令调用存储过程。查询如下:mysql> call do_WhileDemo(10);输出如下+-------+ | ... 阅读更多
107 次查看
您需要使用 rand() 函数从 MySQL 中选择随机结果。语法如下:select *from yourTableName order by rand() limit 1;为了理解以上语法,让我们创建一个表。创建表的查询如下:mysql> create table selectRandomRecord -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20) -> ); Query OK, 0 rows affected (0.53 sec)使用 insert 命令在表中插入一些记录。查询如下:mysql> insert into selectRandomRecord(StudentName) values('John'); Query OK, 1 row affected (0.17 sec) mysql> insert into selectRandomRecord(StudentName) values('Carol'); Query OK, ... 阅读更多
105 次查看
让我们首先创建一个表:mysql> create table LimitWithStoredProcedure -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(10) -> ); Query OK, 0 rows affected (0.47 sec)使用 insert 命令在表中插入一些记录。查询如下:mysql> insert into LimitWithStoredProcedure(Name) values('John'); Query OK, 1 row affected (0.15 sec) mysql> insert into LimitWithStoredProcedure(Name) values('Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into LimitWithStoredProcedure(Name) values('Maxwell'); Query OK, 1 row affected (0.28 sec) mysql> insert into LimitWithStoredProcedure(Name) values('Bob'); Query OK, 1 row affected (0.24 sec) mysql> insert into LimitWithStoredProcedure(Name) values('David'); Query ... 阅读更多
490 次查看
语法如下:update yourTableName set yourColumnName1=yourValue where yourColumnName2=yourValue order by yourIdColumnName DESC LIMIT 1;为了理解以上语法,让我们创建一个表。创建表的查询如下:mysql> create table UpdateWithHighestDemo -> ( -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> UserStatus tinyint, -> UserRank int -> ); Query OK, 0 rows affected (0.61 sec)使用 insert 命令在表中插入一些记录。查询如下:mysql> insert into UpdateWithHighestDemo(UserStatus, UserRank) values(1, 78); Query OK, 1 row affected (0.12 sec) mysql> insert into UpdateWithHighestDemo(UserStatus, UserRank) values(0, 118); Query ... 阅读更多
1K+ 次查看
您可以使用 format() 函数进行分隔符操作。它将在 MySQL 5.5 或更高版本中有效。我们使用的是 8.0.12 版本:mysql> select version(); +-----------+ | version() | +-----------+ | 8.0.12 | +-----------+ 1 row in set (0.00 sec)语法如下:SELECT FORMAT(yourColumnName, valueAfterDecimalPoint, 'de_DE') AS anyAliasNamefrom yourTableName;为了理解以上语法,让我们创建一个表。创建表的查询如下:mysql> create table formatNumberDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Number DECIMAL(19, 1) ... 阅读更多