找到 4379 篇文章 关于 MySQL

MySQL 查询如何从多个 select 语句获取结果?

AmitDiwan
更新于 2019-09-03 08:24:56

641 次查看

要从多个 select 语句获取结果,可以使用 UNION ALL。以下是语法:select yourValue1 AS anyColumnName UNION ALL select yourValue2 AS yourColumnName . . . . N让我们实现上述语法,以便在不同的行中返回数字的枚举:mysql> select 100 AS Number    UNION ALL    select 1000 AS Number    UNION ALL    select 10000 AS Number    UNION ALL    select 100000 AS Number    UNION ALL    select 1000000 AS Number    UNION ALL    select 10000000 AS Number    UNION ALL    select 100000000 AS Number    UNION ALL    select 1000000000 AS Number;这将产生以下输出:-+------------+ | Number     | +------------+ | 100 | | 1000 | | 10000 | | 100000 | | 1000000 | | 10000000 | | 100000000 | | 1000000000 | +------------+ 8 rows in set (0.00 sec)

如何在 MySQL 中选择最后两行?

AmitDiwan
更新于 2019-09-03 08:23:13

7K+ 次查看

要选择最后两行,请使用 ORDER BY DESC LIMIT 2。让我们首先创建一个表:mysql> create table DemoTable763 (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(100) ); Query OK, 0 rows affected (0.57 sec)使用 insert 命令在表中插入一些记录:mysql> insert into DemoTable763(FirstName) values('John'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable763(FirstName) values('Sam'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable763(FirstName) values('Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable763(FirstName) values('David'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable763(FirstName) ... 阅读更多

MySQL 查询如何匹配列值中的任意两个字符串?

AmitDiwan
更新于 2019-09-03 08:21:10

624 次查看

为此,您可以使用带 OR 条件的 LIKE 运算符。让我们首先创建一个表:mysql> create table DemoTable762 (Title text); Query OK, 0 rows affected (0.54 sec)使用 insert 命令在表中插入一些记录:mysql> insert into DemoTable762 values('Introduction to Java'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable762 values('MySQL is a RDBMS'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable762 values('Data Structure and Algorithm in Java'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable762 values('Data Structure and Algorithm in C and C++'); Query OK, 1 row affected (0.18 ... 阅读更多

如何在 MySQL 中查找删除字符串开头字符后跟数字的字符串的总和?

AmitDiwan
更新于 2019-09-03 08:19:12

383 次查看

字符串(列值)以一个字符开头,其余字符串包含数字。我们想要这些数字的总和:J230 A130s C13为此,请使用 SUBSTRING() 函数以及 SUM()。让我们首先创建一个表:mysql> create table DemoTable761 (Price varchar(100)); Query OK, 0 rows affected (0.62 sec)使用 insert 命令在表中插入一些记录:mysql> insert into DemoTable761 values('J230'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable761 values('A130'); Query OK, 1 row affected (0.70 sec) mysql> insert into DemoTable761 values('C13'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable761 values('D456'); Query ... 阅读更多

如何在单个 MySQL 查询中合并查询以获取不同列中不同值的计数?

AmitDiwan
更新于 2019-09-03 08:16:14

171 次查看

让我们首先创建一个表:mysql> create table DemoTable760 (    ClientId int,    ClientId2 int ); Query OK, 0 rows affected (0.79 sec)使用 insert 命令在表中插入一些记录:mysql> insert into DemoTable760 values(100, 200); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable760 values(100, 200); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable760 values(300, 400); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable760 values(300, 400); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable760 values(100, 200); Query OK, 1 row affected (0.23 sec) mysql> insert ... 阅读更多

如何在表列中设置 NULL 值并插入相同的值?

AmitDiwan
更新于 2019-09-03 08:13:26

359 次查看

要设置 NULL 值,请将类型设置为 NULL,如下面的语法所示:yourColumnName dataType NULL;让我们首先创建一个表:mysql> create table DemoTable759 (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(100) NULL ); Query OK, 0 rows affected (0.72 sec)使用 insert 命令在表中插入一些记录:mysql> insert into DemoTable759(FirstName) values('John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable759(FirstName) values(NULL); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable759(FirstName) values('Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable759(FirstName) values(NULL); Query OK, 1 row ... 阅读更多

如何在 MySQL 中为列设置默认值?

AmitDiwan
更新于 2019-09-03 08:09:10

1K+ 次查看

要设置默认值,请使用 DEFAULT 关键字。让我们首先创建一个表:mysql> create table DemoTable758 (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(100) ); Query OK, 0 rows affected (0.66 sec)以下是为列设置默认值的查询:mysql> alter table DemoTable758 add column Colors ENUM('RED', 'GREEN', 'BLUE', 'ORANGE', 'YELLOW') DEFAULT 'YELLOW'; Query OK, 0 rows affected (0.44 sec) Records: 0 Duplicates: 0 Warnings: 0让我们再次检查表的描述:mysql> desc DemoTable758;这将产生以下输出:-+-----------+----------------------------------------------+------+-----+---------+----------------+ | Field     | Type     ... 阅读更多

MySQL 中字符串混合数字的字母数字排序

AmitDiwan
更新于 2019-09-03 08:05:33

470 次查看

假设您在表中有一个 VARCHAR 列,其值为字符串,数字位于右侧。例如:John1023 Carol9871 David9098现在,假设您希望根据整个列中这些右侧的数字进行排序。为此,请使用 ORDER BY RIGHT。让我们首先创建一个表:mysql> create table DemoTable757 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientId varchar(100) ); Query OK, 0 rows affected (0.53 sec)使用 insert 命令在表中插入一些记录:mysql> insert into DemoTable757(ClientId) values('John1023'); Query OK, 1 row ... 阅读更多

如何从 MySQL 中包含多个列的列表中获取最小值?

AmitDiwan
更新于 2019-09-03 08:01:27

1K+ 次查看

让我们首先创建一个表:mysql> create table DemoTable756 (    Value1 int,    Value2 int,    Value3 int ); Query OK, 0 rows affected (0.62 sec)使用 insert 命令在表中插入一些记录:mysql> insert into DemoTable756 values(10, 20, 14); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable756 values(20, 34, 17); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable756 values(200, 134, 789); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable756 values(139, 98, 99); Query OK, 1 row affected (0.17 sec)使用 select 语句显示表中的所有记录 ... 阅读更多

如何在 MySQL 中对别名进行过滤?

AmitDiwan
更新于 2019-09-03 07:57:07

293 次查看

为此,在 HAVING 子句中使用别名。让我们首先创建一个表 -mysql> create table DemoTable755 (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Score1 int,    Score2 int ); Query OK, 0 rows affected (0.62 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable755(Score1, Score2) values(30, 23); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable755(Score1, Score2) values(50, 60); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable755(Score1, Score2) values(89, 90); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable755(Score1, Score2) values(99, 99); Query OK, 1 row ... 阅读更多

广告