0

让我描述一下我的需求......

我有 2 个 MySQL 数据库表。让我将表名称为进货和出库。首先,我在库存中插入了 15 行,每个条目都有一个唯一编号作为序列号。

mysql> select stockin_id, serialno, description from stockin limit 0,15;
+------------+------------+------------------------------------------+
| stockin_id | serialno   | description                              |
+------------+------------+------------------------------------------+
|          1 | KDR0000001 | DATA CARRIER/FLASH CARD WITH RBS 2000 SW |
|          2 | KDR0000002 | DATA CARRIER/FLASH CARD WITH RBS 2000 SW |
|          3 | KDR0000003 | DATA CARRIER/FLASH CARD WITH RBS 2000 SW |
|          4 | KDR0000004 | DATA CARRIER/FLASH CARD WITH RBS 2000 SW |
|          5 | KDR0000005 | DATA CARRIER/FLASH CARD WITH RBS 2000 SW |
|          6 | KDR0000006 | DATA CARRIER/FLASH CARD WITH RBS 2000 SW |
|          7 | KDR0000007 | DATA CARRIER/FLASH CARD WITH RBS 2000 SW |
|          8 | KDR0000008 | DATA CARRIER/FLASH CARD WITH RBS 2000 SW |
|          9 | KDR0000009 | DATA CARRIER/FLASH CARD WITH RBS 2000 SW |
|         10 | KDR0000010 | DATA CARRIER/FLASH CARD WITH RBS 2000 SW |
|         11 | T22ALT5786 | PRINTED BRD ASSMBLY                      |
|         12 | TU82956450 | FUNCTIONAL UNIT/GSM DXU-21               |
|         13 | X510239240 | FUNCTIONAL UNIT/GSM DXU-21               |
|         14 | TD36843873 | PRINTED BOARD ASSEMB/RP4-F               |
|         15 | TD36843908 | PRINTED BOARD ASSEMB/RP4-F               |
+------------+------------+------------------------------------------+
15 rows in set (0.00 sec)

现在,我已经从存储在缺货表中的相同序列号的库存产品中转移了一些产品,如下所示 -

mysql> select stockout_id, serialno, description from stockout limit 0,15;
+------------+------------+------------------------------------------+
| stockin_id | serialno   | description                              |
+------------+------------+------------------------------------------+
|          1 | KDR0000001 | DATA CARRIER/FLASH CARD WITH RBS 2000 SW |
|          2 | KDR0000002 | DATA CARRIER/FLASH CARD WITH RBS 2000 SW |
|          3 | KDR0000003 | DATA CARRIER/FLASH CARD WITH RBS 2000 SW |
|          4 | KDR0000004 | DATA CARRIER/FLASH CARD WITH RBS 2000 SW |
+------------+------------+------------------------------------------+
4 rows in set (0.00 sec)

现在,我想从库存中选择准确的STOCK-IN HAND条目。我怎样才能实现?

4

1 回答 1

0

有几种方法可以做到这一点

SELECT  *
FROM    stockin si  
WHERE   NOT EXISTS  (
                        SELECT  1
                        FROM    stockout so 
                        WHERE   si.serialno = so.serialno
                    )

或者

SELECT  *
FROM    stockin si  
WHERE   si.serialno NOT IN  (
                                SELECT  so.serialno
                                FROM    stockout so 
                            )

如果您知道表中的条目stockinstockout唯一的

SELECT  *
FROM    stockin si LEFT JOIN
        stockout so ON  si.serialno = so.serialno
WHERE   so.serialno IS NULL

我建议 yopu 在阅读时使用对您最有意义的一种,然后才开始担心性能。

于 2013-05-28T04:40:30.347 回答