好的,我有3个表,CLIENT、ORDER、OrderCLIENT。如果客户端在orderClient表中进行了订购,则插入clientid和orderid。但是我可以有没有订单的客户和没有客户的订单,所以orderClient表中没有关于它们的信息,那么我如何在一个查询(MySQL)中选择所有这些呢?我是说
clientid orderid
1 1
null 2
2 null
SELECT*FROW TEST1.CLIENTS;
# clientid, name
'1', 'c1'
'2', 'x1'
SELECT*FROST TEST1.Orders;
# orderid, name
'1', 'ds'
'2', 'd'
从est1.clientorder中选择*;
# clientorderid, clientid, orderid
'1', '1', '1'
这不会在没有订单的情况下返回客户端。从orders
中选择o.*,c.*o左联接
(客户端c左侧加入c.clientid=oc.clientid上的客户顺序oc)在o.orderid=oc.orderid上
# orderid, name, clientid, name
'1', 'ds', '1', 'c1'
'2', 'd', NULL, NULL
以下是要测试的模式-
DROP TABLE IF EXISTS `clientorder`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `clientorder` (
`clientorderid` int NOT NULL,
`clientid` int DEFAULT NULL,
`orderid` int DEFAULT NULL,
PRIMARY KEY (`clientorderid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `clientorder`
--
LOCK TABLES `clientorder` WRITE;
/*!40000 ALTER TABLE `clientorder` DISABLE KEYS */;
INSERT INTO `clientorder` VALUES (1,1,1);
/*!40000 ALTER TABLE `clientorder` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `clients`
--
DROP TABLE IF EXISTS `clients`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `clients` (
`clientid` int NOT NULL,
`name` varchar(3) DEFAULT NULL,
PRIMARY KEY (`clientid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `clients`
--
LOCK TABLES `clients` WRITE;
/*!40000 ALTER TABLE `clients` DISABLE KEYS */;
INSERT INTO `clients` VALUES (1,'c1'),(2,'x1');
/*!40000 ALTER TABLE `clients` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `orders`
--
DROP TABLE IF EXISTS `orders`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `orders` (
`orderid` int NOT NULL,
`name` varchar(3) DEFAULT NULL,
PRIMARY KEY (`orderid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `orders`
--
LOCK TABLES `orders` WRITE;
/*!40000 ALTER TABLE `orders` DISABLE KEYS */;
INSERT INTO `orders` VALUES (1,'ds'),(2,'d');
/*!40000 ALTER TABLE `orders` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2022-09-17 4:20:52
1条答案
按热度按时间wxclj1h51#
您需要完全外部连接,但MyQL不支持它,所以您需要重新构建它
客户端ID|名称|orderid|名称
-|-|
1|c1|1|DS
2|x1|Null|Null
fiddle