Sql查询内部连接表中的不同记录

时间:2017-10-17 23:46:33

标签: sql oracle

所以我加入了几张桌子,我遇到了一些问题。这是我的sql:

SELECT co.orderid,
       orderdate,
       emailaddr,
       city,
       weight,
       partdescription
FROM customer c
INNER JOIN custorder co ON c.customerid = co.customerid
INNER JOIN custorderline cl ON co.orderid = cl.orderid
INNER JOIN inventorypart i ON cl.partnumber = i.partnumber
WHERE orderdate BETWEEN to_date('2015/11/01', 'yyyy/mm/dd') AND to_date('2015/11/30', 'yyyy/mm/dd')
  AND emailaddr LIKE '%gmail%'
  OR emailaddr LIKE '%outlook%'
  AND categoryid IS NOT NULL
  AND weight > 30;

这里有一些结果:

ORDERID    ORDERDATE EMAILADDR                                          CITY                     WEIGHT PARTDESCRIPTION                                  
---------- --------- -------------------------------------------------- -------------------- ---------- --------------------------------------------------
2000000351 30-NOV-15 vacation@gmail.com                                 Walla Walla                  .5 MINI TOWER CASE                                   
2000000351 30-NOV-15 vacation@gmail.com                                 Walla Walla               34.44 EAGLE Z240 XEON E3                                
2000000354 01-DEC-15 jmuench@outlook.com                                Miami                     34.44 EAGLE Z240 XEON E3                                
2000000224 07-OCT-15 findwork@outlook.com                               Asheville                 34.44 EAGLE Z240 XEON E3                                
2000000279 01-NOV-15 analyzeh20@gmail.com                               Sumter                     .438 BOARD GAMES                                       
2000000279 01-NOV-15 analyzeh20@gmail.com                               Sumter                     .375 INTERNET SECURITY                                 
2000000298 08-NOV-15 mswatson@gmail.com                                 Ogden                     27.32 EAGLE PRETIEGE 9000                               
2000000311 12-NOV-15 syarian@gmail.com                                  Whales                       .5 2GB RADEON R7 350 VIDEO CARD                      
2000000316 16-NOV-15 mouse@gmail.com                                    Charleston                 .375 FLASH WING OPTICAL GAMING MOUSE                   
2000000320 17-NOV-15 jjones@gmail.com                                   Burns                      .188 HDMI MALE 180 DEG SWIVEL                          
2000000346 29-NOV-15 sign3@gmail.com                   

这是我想要完成的事情。 11月的所有订单,电子邮件包含gmail或outlook,部件号必须有categoryid,部件必须重量超过30,orderid只显示一次。

我的问题是,重量少于30的部件仍然出现,我不知何故需要这样做“如果订单出现不止一次;只列出一次。”

这是一个令我难以接受的问题......我已经玩了很久了,继续回到它并认为我只是想错了。任何帮助或见解将不胜感激!

1 个答案:

答案 0 :(得分:0)

您需要在WHERE子句中添加括号以处理" OR"逻辑正确。

select co.orderid, orderdate, emailaddr, city, weight, partdescription
  from customer c
 inner join custorder co on c.customerid = co.customerid
 inner join custorderline cl on co.orderid = cl.orderid
 inner join inventorypart i on cl.partnumber = i.partnumber
 where orderdate between to_date('2015/11/01', 'yyyy/mm/dd')
   and to_date('2015/11/30', 'yyyy/mm/dd')
   and ( emailaddr like '%gmail%' or emailaddr like '%outlook%' )
   and categoryid is not null
   and weight > 30;

此查询也不会在" 2015/11/30 00:00"之后获得订单,例如它不会捕获" 2015/11/30 00:01"。 因此,您需要添加时间来格式化掩码:

...AND to_date('2015/11/30 23:59:59', 'yyyy/mm/dd hh24:mi:ss') 

或代替BETWEEN使用

    orderdate >= to_date('2015/11/01', 'yyyy/mm/dd')
and orderdate < to_date('2015/12/01', 'yyyy/mm/dd')