mysqldump where子句使用

mysqldump 的 where 条件子句适用于这种情况:导出某张表的部分数据。

--where=name 参数详情:

-w, --where=name    Dump only selected records. Quotes are mandatory.

使用 where 条件的时候,引号是必须的。where 条件的使用和 sql 语句中 where 是相同的。

语法如下:
1.mysqldump -u username -p password -B dbname --tables tbl --where="clause" > /path/file.sql
or
2.mysqldump -u username -p password --tables dbname tbl --where="clause" > /path/file.sql
or
3.mysqldump -u username -p password -B dbname --tables tbl1 tbl2 --where="clause" > /path/file.sql

注:第3条如果要 dump 多张表,where 条件中涉及的列必须在两张表中都有。

例子:
1.mysqldump -uroot -p -B db1 --tables keith --where="price * count = 10" > /root/keith.sql

表数据

mysql> select * from `keith`;
+----+-------+-------+
| id | count | price |
+----+-------+-------+
|  1 |     1 |    10 |
|  2 |     2 |     5 |
|  3 |     3 |     4 |
|  4 |     2 |     6 |
+----+-------+-------+

备份文件

-- MySQL dump 10.13  Distrib 8.0.27, for Linux (x86_64)
--
-- Host: localhost    Database: db1
-- ------------------------------------------------------
-- Server version       8.0.27
--
-- Table structure for table `keith`
--

DROP TABLE IF EXISTS `keith`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `keith` (
  `id` int NOT NULL AUTO_INCREMENT,
  `count` int NOT NULL,
  `price` int NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `keith`
--
-- WHERE:  price * count = 10

LOCK TABLES `keith` WRITE;
/*!40000 ALTER TABLE `keith` DISABLE KEYS */;
INSERT INTO `keith` VALUES (1,1,10),(2,2,5);
/*!40000 ALTER TABLE `keith` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
-- Dump completed on 2021-12-10 11:30:44

2.mysqldump -uroot -p --tables db1 keith --where="price * count = 10 and count = 2" > /root/keith.sql

备份文件部分内容

--
-- Dumping data for table `keith`
--
-- WHERE:  price * count = 10 and count = 2

LOCK TABLES `keith` WRITE;
/*!40000 ALTER TABLE `keith` DISABLE KEYS */;
INSERT INTO `keith` VALUES (2,2,5);
/*!40000 ALTER TABLE `keith` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

3.mysqldump -uroot -p -B db1 --tables keith wayne --where="price=5" > /root/keith.sql

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值