找到无与伦比的记录mysql

时间:2015-10-07 06:25:17

标签: mysql

我有一个包含OrderId和OrderName列的表订单。

OrderId  |    OrderName
------------------------
  1      |          food
-------------------------
  2      |          milk
----------------------
  3      |     veg
----------------------
  4      |     banana
------------------------

我希望将订单ID与少数随机订单ID匹配,例如3,5,6,2,1。我只想要与5,6不匹配的订单ID。请帮我查询。< / p>

感谢。

1 个答案:

答案 0 :(得分:0)

由于您不想获取订单ID 5和6,因此您可以通过以下查询排除这些订单 -

SELECT * 
FROM Orders 
WHERE OrderID NOT IN(5,6);

根据用户要求进行更新:

    SELECT a.myid as OrderId FROM 
    (
SELECT a.m*10000+b.n*1000+c.p*100+d.q*10+e.r AS  myid FROM 
(SELECT 0 m UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) a
JOIN 
(SELECT 0 n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) b 
JOIN 
(SELECT 0 p UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) c 
JOIN 
(SELECT 0 q UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) d 
JOIN 
(SELECT 0 r UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) e 
ORDER BY myid
    ) a 
    LEFT JOIN orders b ON a.myid=b.orderid 
    WHERE a.myid IN (1,2,3,11,12,6) AND b.orderid IS NULL;

只需将您的订单列表放在a.myid()中并执行以上查询。

此外,如果您自己的订单更多,我们可以相应地修改查询。