mysql缺失值处理_在MySQL中查找/搜索缺失值

bd96500e110b49cbb3cd949968f18be7.png

I am using MySQL and have a table called sales. Its primary key is sales_id.

-------------------------------------

sales_id | invoice_id |

-------------------------------------

1 | 147

2 | 148

3 | 150

For sales_id 3 the invoice_id is supposed to be 149. I want to know which numbers are missing from invoice_id. I start invoice_id at 147 and end invoice_id at 4497. The invoice_id had no relation with sales_id.

Is it possible to know which numbers are missing from invoice_id, using a query?

解决方案

I presume that you have a table of invoice - INVOICES. You may try:

SELECT invoice_id FROM INVOICES invoice

WHERE NOT EXISTS (SELECT * FROM SALES s WHERE invoice.invoice_id = s.invoice_id)

EDIT: If you don't have the INVOICES table, you may need to take all the invoices out before checking if there's any gap.

SELECT DISTINCT invoice_id FROM SALES ORDER BY invoice_id ASC

SELECT MAX(invoice_id) FROM SALES

then, by php:

for ($i = 1; $i < $max_invoice_id; $i++)

{

if (!in_array($i, $all_invoice_id))

{

$gapId[] = $i;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值