Читайте также: |
|
Глава 5. Запрос к нескольким таблицам | ||||||||
_> | ON e1.emp_id | != e2.emp_id | ||||||
_> WHERE | e1.title = 'Teller' AND e2.title = 'Teller'; | |||||||
+__________+__________+____+__________+__________+ | ||||||||
| fname | | lname | | vs | | fname | | lname | | | |||
+__________+__________+____+__________+__________+ | ||||||||
| Sarah | | | Parker | | VS | Chris | | Tucker | | | |||
| Jane | | | Grossman | | VS | | Chris | | Tucker | | | ||
| Thomas | | | Ziegler | | VS | | | Chris | | Tucker | | | |
| Samantha | | | Jameson | | VS | | | Chris | | Tucker | | | |
| Cindy | | | Mason | | VS | | | Chris | | Tucker | | | |
| Frank | | | Portman | | VS | | | Chris | | Tucker | | | |
| Beth | | | Fowler | | VS | | | Chris | | Tucker | | | |
| Rick | | | Tulman | | VS | | | Chris | | Tucker | | | |
| Chris | | | Tucker | | VS | | | Sarah | | Parker | | | |
| Jane | | | Grossman | | VS | | | Sarah | | Parker | | | |
| Thomas | | | Ziegler | | VS | | | Sarah | | Parker | | | |
| Samantha | | | Jameson | | VS | | | Sarah | | Parker | | | |
| Cindy | | | Mason | | VS | | | Sarah | | Parker | | | |
| Frank | | | Portman | | VS | | | Sarah | | Parker | | | |
| Beth | | | Fowler | | VS | | | Sarah | | Parker | | | |
| Rick | | | Tulman | | VS | | Sarah | | Parker | | | ||
... | ||||||||
| Chris | | | Tucker | | VS | Rick | | Tulman | | | |||
| Sarah | | | Parker | | VS | Rick | | Tulman | | | |||
| Jane | | | Grossman | | VS | | Rick | | Tulman | | | ||
| Thomas | | | Ziegler | | VS | | Rick | | Tulman | | | ||
| Samantha | | | Jameson | | VS | | Rick | | Tulman | | | ||
| Cindy | | | Mason | | VS | | Rick | | Tulman | | | ||
| Frank | | | Portman | | VS | | Rick | | Tulman | | | ||
| Beth | | | Fowler | | VS | | Rick | | Tulman | | |
+__________+__________+____+__________+__________+ 72 rows in set (0.01 sec)
Мы на правильном пути, но проблема здесь в том, что для каждой па_ ры (например, Сара Паркер (Sarah Parker) против Криса Такера (Chris Tucker)) имеется «обратная» пара (т. е. Крис Такер против Сары Пар_ кер). Один из способов достигнуть желаемого результата – использо_ вать условие соединения e1.emp_id < e2.emp_id, чтобы каждый опера_ ционист входил в пару только с теми, у кого ID сотрудника больше (можно также использовать e1.emp_id > e2.emp_id, если вам так больше нравится):
mysql> | SELECT e1.fname, | e1.lname, 'VS' vs, e2.fname, e2.lname | |
_> | FROM employee e1 | INNER JOIN employee e2 | |
_> | ON e1.emp_id | < | e2.emp_id |
_> | WHERE e1.title | = | 'Teller' AND e2.title = 'Teller'; |
+__________+__________+____+__________+__________+ | fname | lname | vs | fname | lname | +__________+__________+____+__________+__________+
| | Chris | | | Tucker | | | VS | | | Sarah | | | Parker | |
| | Chris | | | Tucker | | | VS | | | Jane | | | Grossman | |
Сравнение условий соединения и условий фильтрации | |||||
| Chris | | Tucker | | VS | | Thomas | | Ziegler | | |
| Chris | | Tucker | | VS | | Samantha | | Jameson | | |
| Chris | | Tucker | | VS | | Cindy | | Mason | | |
| Chris | | Tucker | | VS | | Frank | | Portman | | |
| Chris | | Tucker | | VS | | Beth | | Fowler | | |
| Chris | | Tucker | | VS | | Rick | | Tulman | | |
| Sarah | | Parker | | VS | | Jane | | Grossman | | |
| Sarah | | Parker | | VS | | Thomas | | Ziegler | | |
| Sarah | | Parker | | VS | | Samantha | | Jameson | | |
| Sarah | | Parker | | VS | | Cindy | | Mason | | |
| Sarah | | Parker | | VS | | Frank | | Portman | | |
| Sarah | | Parker | | VS | | Beth | | Fowler | | |
| Sarah | | Parker | | VS | | Rick | | Tulman | | |
| Jane | | Grossman | | VS | | Thomas | | Ziegler | | |
| Jane | | Grossman | | VS | | Samantha | | Jameson | | |
| Jane | | Grossman | | VS | | Cindy | | Mason | | |
| Jane | | Grossman | | VS | | Frank | | Portman | | |
| Jane | | Grossman | | VS | | Beth | | Fowler | | |
| Jane | | Grossman | | VS | | Rick | | Tulman | | |
| Thomas | | Ziegler | | VS | | Samantha | | Jameson | | |
| Thomas | | Ziegler | | VS | | Cindy | | Mason | | |
| Thomas | | Ziegler | | VS | | Frank | | Portman | | |
| Thomas | | Ziegler | | VS | | Beth | | Fowler | | |
| Thomas | | Ziegler | | VS | | Rick | | Tulman | | |
| Samantha | | Jameson | | VS | | Cindy | | Mason | | |
| Samantha | | Jameson | | VS | | Frank | | Portman | | |
| Samantha | | Jameson | | VS | | Beth | | Fowler | | |
| Samantha | | Jameson | | VS | | Rick | | Tulman | | |
| Cindy | | Mason | | VS | | Frank | | Portman | | |
| Cindy | | Mason | | VS | | Beth | | Fowler | | |
| Cindy | | Mason | | VS | | Rick | | Tulman | | |
| Frank | | Portman | | VS | | Beth | | Fowler | | |
| Frank | | Portman | | VS | | Rick | | Tulman | | |
| Beth | | Fowler | | VS | | Rick | | Tulman | | |
+__________+__________+____+__________+__________+ 36 rows in set (0.01 sec)
Теперь у нас есть список из 36 пар. Как раз столько, сколько должно быть при наличии девяти участников.
Дата добавления: 2015-08-17; просмотров: 46 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
Сравнение эквисоединений с неэквисоединениями | | | Сравнение условий соединения и условий фильтрации |