Mysql 不存在记录的多对多关系查询
这对你来说是一个巨大的挑战 我们正在尝试为多对多数据库创建一个查询,有许多客户机和许多订单,记录表就是联合表。我们需要从Orders表中选择clientid等于指定id的所有行,例如1、2或10等,其中也没有该订单的记录Mysql 不存在记录的多对多关系查询,mysql,Mysql,这对你来说是一个巨大的挑战 我们正在尝试为多对多数据库创建一个查询,有许多客户机和许多订单,记录表就是联合表。我们需要从Orders表中选择clientid等于指定id的所有行,例如1、2或10等,其中也没有该订单的记录 -- phpMyAdmin SQL Dump -- version 5.0.1 -- https://www.phpmyadmin.net/ -- -- Host: 127.0.0.1 -- Generation Time: Oct 09, 2020 at 10:14 AM -
-- phpMyAdmin SQL Dump
-- version 5.0.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Oct 09, 2020 at 10:14 AM
-- Server version: 10.4.11-MariaDB
-- PHP Version: 7.3.15
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `lb`
--
-- --------------------------------------------------------
--
-- Table structure for table `clients`
--
CREATE TABLE `clients` (
`id` int(11) NOT NULL,
`clientname` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `clients`
--
INSERT INTO `clients` (`id`, `clientname`) VALUES
(1, 'john'),
(2, 'sue');
-- --------------------------------------------------------
--
-- Table structure for table `links`
--
CREATE TABLE `links` (
`id` int(11) NOT NULL,
`linkname` varchar(255) NOT NULL,
`da` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `links`
--
INSERT INTO `links` (`id`, `linkname`, `da`) VALUES
(1, 'ice cream', 0),
(2, 'pizza', 0),
(3, 'pasta', 0);
-- --------------------------------------------------------
--
-- Table structure for table `records`
--
CREATE TABLE `records` (
`id` int(11) NOT NULL,
`date` timestamp NULL DEFAULT NULL,
`linkid` int(11) NOT NULL,
`clientid` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `records`
--
INSERT INTO `records` (`id`, `date`, `linkid`, `clientid`) VALUES
(1, NULL, 2, 1),
(2, NULL, 2, 2);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `clients`
--
ALTER TABLE `clients`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `links`
--
ALTER TABLE `links`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `records`
--
ALTER TABLE `records`
ADD PRIMARY KEY (`id`),
ADD KEY `linkindex` (`linkid`),
ADD KEY `clientindex` (`clientid`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `clients`
--
ALTER TABLE `clients`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
--
-- AUTO_INCREMENT for table `links`
--
ALTER TABLE `links`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=54;
--
-- AUTO_INCREMENT for table `records`
--
ALTER TABLE `records`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `records`
--
ALTER TABLE `records`
ADD CONSTRAINT `records_ibfk_1` FOREIGN KEY (`clientid`) REFERENCES `clients` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `records_ibfk_2` FOREIGN KEY (`linkid`) REFERENCES `links` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
COMMIT;
/*!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 */;
SELECT links.id, links.linkname
FROM links
LEFT JOIN records ON records.linkid = links.id
WHERE records.linkid IS NULL AND records.clientid != 1
所需的输出是所有订单的列表,例如Sue尚未下订单
Sue(ID 2)订购了比萨饼,因此查询结果应该只显示意大利面和冰淇淋
id linkname
1 ice cream
3 pasta
我们本以为这会奏效,但似乎我们错过了什么
提前感谢您具有出色的问题解决能力 用完整的创建表脚本替换短表定义。提供一些示例数据作为插入到脚本中的数据,并显示此数据的所需结果。将WHERE to和Change and WHERE更改为1)我没有看到所提供数据的所需输出作为输出表。2) 你的脚本不正确。编辑它直到可行,调整MySQL版本,并提供链接。对我来说很好