找到 4379 篇文章 适用于 MySQL
679 次查看
要连接字符串字段,请使用 CONCAT() 函数。让我们首先创建一个表 -mysql> create table DemoTable -> ( -> SequenceId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentId varchar(100) -> ); Query OK, 0 rows affected (0.59 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable(StudentId) values('STU'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentId) values('STU1'); Query OK, 1 row affected (0.18 sec)使用 select 语句显示表中的所有记录 -mysql> select *from DemoTable;Output+------------+-----------+ | SequenceId | StudentId | +------------+-----------+ | 1 ... 阅读更多
336 次查看
要从 DOB 获取年龄,您可以使用 TIMESTAMPDIFF() 函数。以下是语法 -select TIMESTAMPDIFF(YEAR, yourColumnName, CURRENT_DATE) AS anyAliasName from yourTableName;让我们首先创建一个表 -mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> DateOfBirth datetime -> ); Query OK, 0 rows affected (0.73 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable(DateOfBirth) values('1998-06-04 12:30:00'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(DateOfBirth) values('2000-01-31 04:10:20'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(DateOfBirth) values('2010-12-01 03:50:45'); ... 阅读更多
412 次查看
您可以为此使用 UNION ALL。让我们将列表 10、20、30、40、50 作为带有 UNION ALL 的表 -mysql> select 10 Number UNION ALL select 20 Number UNION ALL select 30 Number UNION ALL select 40 Number UNION ALL select 50 Number;Output+--------+ | Number | +--------+ | 10 | | 20 | | 30 | | 40 | | 50 | +--------+ 5 rows in set (0.00 sec)让我们看看另一个例子。要将列表 1、2、3 作为表,请使用以下查询 -mysql> SELECT 1 a UNION ALL SELECT 2 a UNION ALL SELECT 3 a;Output+---+ | a | +---+ | 1 | | 2 | | 3 | +---+ 3 rows in set (0.00 sec)
137 次查看
为此,您可以使用 LEAST()。以下是语法 -select least(yourColumnName, yourMaxValue) from yourTableName;让我们首先创建一个表 -mysql> create table DemoTable -> ( -> Number int -> ); Query OK, 0 rows affected (0.64 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable values(50); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(201); Query OK, 1 row affected (0.12 sec) ... 阅读更多
254 次查看
为此,您可以使用 DATEDIFF() 函数。当前日期时间如下 -mysql> select now(); +---------------------+ | now() | +---------------------+ | 2019-06-09 19:15:56 | +---------------------+ 1 row in set (0.00 sec)让我们首先创建一个表 -mysql> create table DemoTable -> ( -> ShippingDate datetime -> ); Query OK, 0 rows affected (0.66 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable values('2019-06-01'); Query OK, 1 row affected (0.19 sec) ... 阅读更多
112 次查看
要计算三个以上列值是否为真,您可以使用 WHERE 子句。让我们首先创建一个表 -mysql> create table DemoTable -> ( -> isMarried boolean, -> isActive boolean, -> isMember boolean, -> isOn boolean -> ); Query OK, 0 rows affected (0.61 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable values(true, false, true, false); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(false, false, false, false); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(true, true, ... 阅读更多
2K+ 次查看
让我们首先创建一个表 -mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100) -> ); Query OK, 0 rows affected (0.67 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Name) values('David'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Name) values('Carol'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Name) values('Bob'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Name) ... 阅读更多
140 次查看
让我们首先创建一个表。我们在创建表时使用了 AUTO_INCREMENT 为 StudentId 设置自动递增 -mysql> create table DemoTable -> ( -> StudentId int NOT NULL AUTO_INCREMENT, -> StudentFirstName varchar(100), -> StudentLastName varchar(100), -> StudentAge int, -> StudentCountryName varchar(100), -> PRIMARY KEY(StudentId) -> )AUTO_INCREMENT=30; Query OK, 0 rows affected (0.69 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable(StudentFirstName, StudentLastName, StudentAge, StudentCountryName) values('John', 'Smith', 21, 'US'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(StudentFirstName, StudentLastName, StudentAge, StudentCountryName) values('Chris', 'Brown', ... 阅读更多
198 次查看
让我们首先创建一个表 -mysql> create table DemoTable -> ( -> Id int, -> Value int -> ); Query OK, 0 rows affected (0.68 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable values(100, 85885); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(101, 885995474); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(100, 895943); Query OK, 1 row affected (0.20 sec)使用 select 语句显示表中的所有记录 -mysql> select *from DemoTable;Output+------+-----------+ | Id | ... 阅读更多
431 次查看
为此,您可以使用 GROUP BY 并使用 COUNT 获取仅非重复值。以下是语法 -select yourColumnName from yourTableName group by yourColumnName having count(*)=1;让我们首先创建一个表 -mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Subject varchar(100) -> ); Query OK, 0 rows affected (0.70 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable(Subject) values('MySQL'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.14 sec) mysql> insert into ... 阅读更多