【问题标题】:Show difference between two tables in mysql using join [duplicate]使用连接显示mysql中两个表之间的差异[重复]
【发布时间】:2021-10-14 21:38:42
【问题描述】:

我的数据库中有两个表:项目和发票

表格发票

CREATE TABLE `invoices` (
  `id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL
);


INSERT INTO `invoices` (`id`, `name`) VALUES
(1, 'Invoice 1'),
(2, 'Invoice 2'),
(3, 'Invoice 3');

表格项目

CREATE TABLE `projects` (
  `id` int(11) NOT NULL,
  `invoice_id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Fazendo dump de dados para tabela `projects`
--

INSERT INTO `projects` (`id`, `invoice_id`, `name`) VALUES
(1, 1, 'Project name');

预期结果: 显示行

(2, 'Invoice 2'),
(3, 'Invoice 3')

【问题讨论】:

    标签: mysql sql join


    【解决方案1】:

    如果您想要没有项目有该发票的发票,那么一种方法使用not exists

    select i.*
    from invoices i
    where not exists (select 1
                      from projects p
                      where p.invoice_id = i.id
                     );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-12
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多