如果t1.uniqueNumber!= t2.uniqueNumber,则从t1中选择全部

时间:2013-05-28 04:37:26

标签: mysql select

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

我有2个数据库的MySQL表。让我将表名称称为库存和缺货。首先,我在库存中插入了15行,每个条目的唯一编号为serialno。

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)

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

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 条目。我怎么能实现?

1 个答案:

答案 0 :(得分:0)

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

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

OR

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在阅读时使用对你最有意义的那个,然后才开始担心性能。