2017-11-29 12 views
0

저는 mysql을 처음 사용하고 sakila 데이터베이스를 가지고 놀고 있습니다. 누구도 이러한 쿼리가 정확히 동일한 행을 반환하지 않는 이유에 대해 어떤 힌트를 제공 할 수 있습니까? 내가 IN()를 사용하는 경우MYSQL : IN과 OR이 같은 행을 반환하지 않습니다

mysql> use sakila 
Database changed 
mysql> SELECT first_name,last_name,address.city_id FROM customer 
    -> JOIN store ON store.store_id=customer.store_id 
    -> JOIN address ON customer.address_id=address.address_id 
    -> WHERE store.store_id=1 AND address.city_id=1 OR address.city_id=42 
    -> OR address.city_id=312 OR address.city_id=459; 
+------------+-------------+---------+ 
| first_name | last_name | city_id | 
+------------+-------------+---------+ 
| JULIE  | SANCHEZ  |  1 | 
| SCOTT  | SHELLEY  |  42 | 
| CLINTON | BUFORD  |  42 | 
| MATTIE  | HOFFMAN  |  312 | 
| CECIL  | VINES  |  312 | 
| NELSON  | CHRISTENSON |  459 | 
+------------+-------------+---------+ 
6 rows in set (0,00 sec) 

: 나는의 순서 OR 문을 사용하는 경우

mysql> SELECT first_name,last_name,address.city_id FROM customer 
    -> JOIN store ON store.store_id=customer.store_id 
    -> JOIN address ON customer.address_id=address.address_id 
    -> WHERE store.store_id=1 AND address.city_id IN (1,42,312,459); 
+------------+-------------+---------+ 
| first_name | last_name | city_id | 
+------------+-------------+---------+ 
| JULIE  | SANCHEZ  |  1 | 
| SCOTT  | SHELLEY  |  42 | 
| CECIL  | VINES  |  312 | 
| NELSON  | CHRISTENSON |  459 | 
+------------+-------------+---------+ 
4 rows in set (0,00 sec) 

은 IN이() CITY_ID의 첫 번째 인스턴스를 나열 보인다. 웹상의 모든 곳에서이 두 시나리오는 성능면에서만 차이가 있다고 명시되어 있습니다. 나는 무엇을 놓치고 있습니까?

+0

당신은 괄호가 누락되었습니다. –

답변

4

ANDOR 조합을 round brackets()에 첫 번째 쿼리에 적절하게 입력해야합니다.

조항 WHERE store.store_id=1 AND address.city_id=1 OR address.city_id=42 OR address.city_id=312 OR address.city_id=459은 store_id가 1이고 city_id가 1 인 모든 행과 store_id와 관계없이 city_id가 41 또는 312 또는 459 인 모든 행을 반환합니다.

아래 쿼리로 변경하면 두 번째 IN 쿼리와 완전히 동일한 행이 반환됩니다.

SELECT first_name,last_name,address.city_id FROM customer 
JOIN store ON store.store_id=customer.store_id 
JOIN address ON customer.address_id=address.address_id 
WHERE store.store_id=1 
AND (address.city_id=1 
    OR address.city_id=42 
    OR address.city_id=312 
    OR address.city_id=459); 
+0

위대한, 감사합니다 사미르! 내가 store.store_id를 SELECT 할 때 상황이 완벽하게 명확 해집니다! – mlcr

+0

당신이 내 대답을 받아 들일 수도 있습니다. 그래서 누군가가 미래에 나타나면 더 유용해질 것입니다. :) – Samir

1

두 번째 쿼리에 사용 된 AND는 두 조건에서만 작동하기 때문에 쿼리에서 괄호를 사용해야합니다. 이것을 시도해야합니다

SELECT first_name,last_name,address.city_id FROM customer 
JOIN store ON store.store_id=customer.store_id 
JOIN address ON customer.address_id=address.address_id 
WHERE store.store_id=1 AND 
(
    address.city_id = 1 
    OR address.city_id = 42 
    OR address.city_id = 312 
    OR address.city_id = 459 
); 

두 쿼리가 모두 같은 결과를 반환하면이 결과를 반환해야합니다. 와 쿼리를 교체

1

,

mysql> select first_name,last_name,address.city_id from customer 
     ->join store on store.store_id=customer.store_id 
     -> join address on customer.address_id=address.address_id 
     -> where store.store_id=1 and (address.city_id = 1 or address.city_id = 42 or address.city_id = 312 or address.city_id = 459);