找到 4379 篇文章 关于 MySQL
517 次查看
为此,您可以使用 ORDER BY FIELD。让我们首先创建一个表 -mysql> create table DemoTable -> ( -> Id int, -> Name varchar(20) -> ); Query OK, 0 rows affected (1.78 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable values(101, 'Chris'); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable values(201, 'Mike'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(110, 'Adam'); Query OK, 1 row affected (0.52 sec) mysql> insert into DemoTable values(250, 'John'); Query OK, 1 row affected (0.33 sec)显示所有 ... 阅读更多
484 次查看
为此,在单个 MySQL 查询中多次使用 UNION。让我们首先创建一个表 -mysql> create table DemoTable -> ( -> Value1 int, -> Value2 int, -> Value3 int -> ); Query OK, 0 rows affected (0.69 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable values(20, null, null); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(20, null, null); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(20, null, null); Query OK, 1 row affected (0.12 sec) mysql> insert into ... 阅读更多
153 次查看
带有 WHEN 子句的 CASE 语句用于处理条件。以下是语法 -select *, case when yourCondition then yourStatement when yourCondition then yourStatement . . else yourStatement from yourTableName;让我们首先创建一个表 -mysql> create table DemoTable -> ( -> StudentName varchar(20), -> StudentMarks int -> ); Query OK, 0 rows affected (0.77 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable values('Chris', 78); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Robert', 88); Query OK, 1 row affected (0.14 sec) ... 阅读更多
69 次查看
要替换重复记录并在插入时避免任何错误,请使用 INSERT ON DUPLICATE KEY UPDATE。让我们首先创建一个表 -mysql> create table DemoTable -> ( -> Id int, -> Name varchar(20), -> UNIQUE(Id, Name) -> ); Query OK, 0 rows affected (0.78 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable values(101, 'Chris') on duplicate key update Id=10001, Name='Robert'; Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(102, 'Mike') on duplicate key update Id=10001, Name='Robert'; Query OK, 1 row affected (0.17 sec) mysql> insert into ... 阅读更多
3K+ 次查看
是的,我们可以使用 MySQL GROUP_CONCAT() 将列记录转换为列表。让我们首先创建一个表 -mysql> create table DemoTable -> ( -> ClientId int, -> ClientName varchar(20) -> ); Query OK, 0 rows affected (0.88 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.54 sec) mysql> insert into DemoTable values(100, 'Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(100, 'Adam'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(100, 'David'); Query OK, ... 阅读更多
621 次查看
要创建包含日期的临时表,请在 MySQL 中使用 CREATE TEMPORARY TABLE。以下是语法 -语法create temporary table yourTableName( yourColumnName datetime );让我们首先创建一个表 -mysql> create temporary table DemoTable -> ( -> DueDate datetime -> ); Query OK, 0 rows affected (0.00 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable values(now()); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable values(curdate()); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable values('2018-01-21'); Query OK, 1 row affected (0.00 sec) mysql> insert into ... 阅读更多
736 次查看
要在一个查询中为多个项目增加商品价值,您可以在 MySQL 中使用 CASE 语句。让我们首先创建一个表 -mysql> create table DemoTable -> ( -> ProductName varchar(20), -> ProductPrice int -> ); Query OK, 0 rows affected (0.51 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable values('Product-1', 700); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Product-2', 1000); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Product-3', 3000); Query OK, 1 row affected (0.10 sec)显示所有记录 ... 阅读更多
1K+ 次查看
为此,您可以使用 GROUP BY HAVING 以及 COUNT(*) 函数。让我们首先创建一个表 -mysql> create table DemoTable -> ( -> Value int -> ); Query OK, 0 rows affected (0.47 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.16 sec) mysql> insert into ... 阅读更多
398 次查看
最好使用 IN() 而不是多个 OR 语句。让我们首先创建一个表 -mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> FirstName varchar(20) -> ); Query OK, 0 rows affected (0.83 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName) values('Adam'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable(FirstName) values('David'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(FirstName) values('Mike'); Query OK, 1 ... 阅读更多
2K+ 次查看
要在 MySQL 中实现自定义排序顺序,需要使用 ORDER BY FIELD()。 让我们先创建一个表 -mysql> create table DemoTable -> ( -> Designation varchar(100) -> ); Query OK, 0 rows affected (1.65 sec)使用 insert 命令在表中插入一些记录 -mysql> insert into DemoTable values('Software Engineer'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Associate Software Engineer'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('Software Development Engineer'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Product Manager'); Query OK, 1 row ... 阅读更多