我的发票软件需要很长时间才能启动,因此我正在尝试清除旧数据(与我当前的'BusinessID'无关的任何内容),但我被卡住了!
我有3个表 - 我已经列出了每个表的相关列:
公司(客户): ID, clientOfBusinessID
发票: ID, invoiceOfClientID
invoice_line_items lineItemOfInvoiceID,
我想删除与我当前业务无关的所有* invoice_line_items *行。
以下声明似乎向我展示了与我当前业务无关的所有客户:
companies.clientOfBusinessID NOT LIKE '%41A1%'
我该怎么做?我玩过JOIN查询,但不能完全结合在一起。
说实话,我不知道我在做什么,只是在线阅读指南,这是我能想到的最好的 - 根本不起作用!
DELETE from invoice_line_items
FROM invoices JOIN companies ON invoices.invoiceOfClientID = companies.id
JOIN invoice_line_items ON invoices.id = invoice_line_items.lineItemOfInvoiceID
WHERE companies.clientOfBusinessID NOT LIKE '%41A1%'
有什么想法吗?
非常感谢
答案 0 :(得分:1)
此查询(如果我理解您的结构正确)将为您提供与您的公司无关的所有发票:
SELECT i.*
FROM invoices i
INNER JOIN companies c
ON i.invoiceOfClientID = c.id
WHERE c.clientOfBusinessID NOT LIKE '%41A1%'
要删除发票行项目,我们可以按如下方式使用上述查询:
DELETE from invoice_line_items
WHERE lineItemOfInvoiceID IN
(SELECT i.id
FROM invoices i
INNER JOIN companies c
ON i.invoiceOfClientID = c.id
WHERE c.clientOfBusinessID NOT LIKE '%41A1%')
您可能还想删除发票:
DELETE from invoices
WHERE id IN
(SELECT i.id
FROM invoices i
INNER JOIN companies c
ON i.invoiceOfClientID = c.id
WHERE c.clientOfBusinessID NOT LIKE '%41A1%')