简体   繁体   English

从SELECT查询中获得相反的结果

[英]get the opposite results from a SELECT query

these are my tables: 这些是我的表:

`room`(roomID,roomNum)  
`customer`(customerID,Surname,etc)  
`contract`(contractID,roomID,weekNum)  
`paymen`t(paymentID,customerID,contractID,YearKoino)  

and when i use the following query: 当我使用以下查询时:

`select` room.roomnum  
`from` payment,contract,room,customer  
`where` payment.contractID = contract.contractID  
`and` contract.roomID=room.roomID  
`and` customer.customerID=payment.customerID  
`and` contract.weeknum='40'  
`and` payment.YearKoino='2007' ;  

the results i am getting are: 我得到的结果是:

+---------+  
| roomnum |  
+---------+  
| Δ-12    |  
| Γ-22    |  
| Α-32    |  
| Γ-21    |  
| Δ-11    |  
| Ε-12    |  
| Γ-31    |  
| Ε-22    |  
| Α-22    |  
| Δ-12    |  
| Γ-12    |  
+---------+  
11 rows in set  

what i want to do is to run a query that gives me the exact opposite results(roomnums in table room that are not in table payment).This can be done by comparing the roomum results from the above query with the column roomnum in the room table.some of my efforts so far : 我想做的是运行一个查询,该查询给出与我完全相反的结果(表房中不包含在表付款中的Roomnum),这可以通过将上述查询的Roomum结果与房间中的Roomnum列进行比较来完成table。到目前为止,我的一些努力:

`Select` room.roomnum  
`from` room  
`where` NOT EXISTS  
(`select` room.roomnum  
`from` payment,contract,room,customer  
`where` payment.contractID = contract.contractID  
`and` contract.roomID=room.roomID  
`and` customer.customerID=payment.customerID  
`and` contract.weeknum='40'  
`AND` payment.YearKoino='2007');  
Empty set  

and

`SELECT` *
`FROM` customer a
`LEFT OUTER JOIN` payment b
`on` a.customerID=b.customerID
`where` a.customer is null;

i also tried to replace the "NOT EXISTS" with the "NOT IN" but in vain.I have read that the best way to do that is by using the "left join".well i can do it when i have to compare to simple tables.but in my example i have to compare a column with a column that is in a join of tables... 我也尝试用“ NOT IN”代替“ NOT EXISTS”,但徒劳。我已经读到,做到这一点的最佳方法是使用“ left join”。当我不得不比较时,我可以做到这一点。简单的表。但在我的示例中,我必须将列与表联接中的列进行比较...

any advice would be appreciated. 任何意见,将不胜感激。

I am not sure why your not in didn't work. 我不确定为什么您not in

This should work (using not in with table name aliases): 这应该可以工作(不要与表名别名一起使用):

   Select r1.roomnum 
   from room AS r1
   where r1.roomnum NOT IN 
       (select r2.roomnum 
        from payment,contract,room as r2,customer 
        where payment.contractID = contract.contractID
        and contract.roomID=r2.roomID 
        and customer.customerID=payment.customerID 
        and contract.weeknum='40' 
        AND payment.YearKoino='2007');

Of course you must correlate your NOT EXISTS query with your main query 当然,你必须关联您的NOT EXISTS查询与主查询

Select 
  roomnum 
from 
  room main
where 
  NOT EXISTS (
   select 1 
   from   payment
          inner join contract on payment.contractID = contract.contractID
          inner join room     on contract.roomID = room.roomID 
          inner join customer on customer.customerID = payment.customerID 
   where  contract.weeknum='40' 
          and payment.YearKoino='2007'
          and room.roomnum = main.roomnum  -- < correlation to main query
);

Also, learn SQL-92 style joins. 另外,学习SQL-92样式的联接。 Nobody does old style joins anymore. 没有人会再加入旧样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM