简体   繁体   中英

LEFT JOIN shows different results from RIGHT JOIN - when joining table on itself… why?

I have a single taxonomies table with parent->child hierarchical data. I thought that queries that I post below should return identical result because I'm joining the same table on itself - but I'm getting different results. why?

Here's my left join query and it's result:

SELECT a.id AS cat_id, b.id AS subcat_id, a.name AS cat_name, b.name AS sub_cat_name, CONCAT_WS(' / ', a.name, b.name) AS full_name 
FROM taxonomies as a
LEFT JOIN taxonomies AS b 
ON a.id = b.parent_taxonomy_id 
WHERE (a.name LIKE 'Se%' OR b.name LIKE 'Se%')
ORDER BY full_name DESC;

+--------+-----------+----------+--------------+--------------------+
| cat_id | subcat_id | cat_name | sub_cat_name | full_name          |
+--------+-----------+----------+--------------+--------------------+
|     84 |        85 | Season   | Winter       | Season / Winter    |
|     84 |        91 | Season   | Summer       | Season / Summer    |
|     84 |        90 | Season   | Spring       | Season / Spring    |
|     84 |       128 | Season   | Fall         | Season / Fall      |
|     84 |       129 | Season   | Christmas    | Season / Christmas |
+--------+-----------+----------+--------------+--------------------+
5 rows in set (0.00 sec)

```

Here's my right join query and it's result:

SELECT a.id AS cat_id, b.id AS subcat_id, a.name AS cat_name, b.name AS sub_cat_name, CONCAT_WS(' / ', a.name, b.name) AS full_name 
FROM taxonomies as a
RIGHT JOIN taxonomies AS b 
ON a.id = b.parent_taxonomy_id 
WHERE (a.name LIKE 'Se%' OR b.name LIKE 'Se%')
ORDER BY full_name DESC;

+--------+-----------+----------+--------------+--------------------+
| cat_id | subcat_id | cat_name | sub_cat_name | full_name          |
+--------+-----------+----------+--------------+--------------------+
|     84 |        85 | Season   | Winter       | Season / Winter    |
|     84 |        91 | Season   | Summer       | Season / Summer    |
|     84 |        90 | Season   | Spring       | Season / Spring    |
|     84 |       128 | Season   | Fall         | Season / Fall      |
|     84 |       129 | Season   | Christmas    | Season / Christmas |
|   NULL |        84 | NULL     | Season       | Season             |
+--------+-----------+----------+--------------+--------------------+
6 rows in set (0.00 sec)

Here's the data in taxonomies table:

mysql> select * from taxonomies;
+-----+-----------------------+-----------------------+--------+----------------+--------------------+---------+---------------------+---------------------+
| id  | name                  | machine_name          | app_id | is_subcategory | parent_taxonomy_id | user_id | created_at          | updated_at          |
+-----+-----------------------+-----------------------+--------+----------------+--------------------+---------+---------------------+---------------------+
|  84 | Season                | season                | mw     |              0 |               NULL |   94711 | 2015-04-10 12:00:00 | 2015-04-10 12:00:00 |
|  85 | Winter                | winter                | mw     |              1 |                 84 |   94711 | 2015-04-10 12:00:00 | 2015-04-10 12:00:00 |
|  90 | Spring                | spring                | mw     |              1 |                 84 |   94711 | 2015-04-10 12:00:00 | 2015-04-10 12:00:00 |
|  91 | Summer                | summer                | mw     |              1 |                 84 |   94711 | 2015-04-10 12:00:00 | 2015-04-10 12:00:00 |
| 128 | Fall                  | fall                  | mw     |              1 |                 84 |   94711 | 2015-07-09 13:18:03 | 2015-07-09 13:18:03 |
| 129 | Christmas             | christmas             | mw     |              1 |                 84 |   94711 | 2015-07-09 13:18:11 | 2015-07-09 13:18:11 |
| 130 | Content Type          | content-type          | mw     |              0 |               NULL |   94711 | 2015-07-09 13:18:47 | 2015-07-09 13:18:47 |
| 131 | Trend Watch           | trend-watch           | mw     |              1 |                130 |   94711 | 2015-07-09 13:19:10 | 2015-07-09 13:19:10 |
| 132 | Charm Unit            | charm-unit            | mw     |              1 |                130 |   94711 | 2015-07-09 13:19:17 | 2015-07-09 13:19:17 |
| 133 | Infographic           | infographic           | mw     |              1 |                130 |   94711 | 2015-07-09 13:19:23 | 2015-07-09 13:19:23 |
| 134 | Word Art              | word-art              | mw     |              1 |                130 |   94711 | 2015-07-09 13:19:29 | 2015-07-09 13:19:29 |
| 135 | Storify               | storify               | mw     |              1 |                130 |   94711 | 2015-07-09 13:19:35 | 2015-07-09 13:19:35 |
| 136 | Content Label         | content-label         | mw     |              0 |               NULL |   94711 | 2015-07-09 13:19:59 | 2015-07-09 13:19:59 |
| 137 | Usage                 | usage                 | mw     |              1 |                136 |   94711 | 2015-07-09 13:20:04 | 2015-07-09 13:20:04 |
| 138 | Word History          | word-history          | mw     |              1 |                136 |   94711 | 2015-07-09 13:20:10 | 2015-07-09 13:20:10 |
| 139 | Spelling Bee          | spelling-bee          | mw     |              1 |                136 |   94711 | 2015-07-09 13:20:16 | 2015-07-09 13:20:16 |
| 140 | Obscure Words         | obscure-words         | mw     |              1 |                136 |   94711 | 2015-07-09 13:20:22 | 2015-07-09 13:20:22 |
| 141 | Words We're Watching  | words-were-watching   | mw     |              1 |                136 |   94711 | 2015-07-09 13:20:29 | 2015-07-09 13:20:29 |
| 142 | Word Lists            | word-lists            | mw     |              1 |                136 |   94711 | 2015-07-09 13:20:34 | 2015-07-09 13:20:34 |
| 143 | Language Acquisition  | language-acquisition  | mw     |              1 |                136 |   94711 | 2015-07-09 13:20:41 | 2015-07-09 13:20:41 |
| 144 | Trending              | trending              | mw     |              1 |                136 |   94711 | 2015-07-09 13:20:45 | 2015-07-09 13:20:45 |
| 145 | Best Of               | best-of               | mw     |              1 |                136 |   94711 | 2015-07-09 13:20:50 | 2015-07-09 13:20:50 |
| 146 | Pop Culture           | pop-culture           | mw     |              1 |                136 |   94711 | 2015-07-09 13:20:55 | 2015-07-09 13:20:55 |
| 147 | Politics              | politics              | mw     |              1 |                136 |   94711 | 2015-07-09 13:21:01 | 2015-07-09 13:21:01 |
| 148 | Science               | science               | mw     |              1 |                136 |   94711 | 2015-07-09 13:21:06 | 2015-07-09 13:21:06 |
| 149 | Grammar               | grammar               | mw     |              1 |                136 |   94711 | 2015-07-09 13:21:10 | 2015-07-09 13:21:10 |
| 150 | Travel                | travel                | mw     |              1 |                136 |   94711 | 2015-07-09 13:21:14 | 2015-07-09 13:21:14 |
| 151 | History               | history               | mw     |              1 |                136 |   94711 | 2015-07-09 13:21:20 | 2015-07-09 13:21:20 |
| 152 | Sports                | sports                | mw     |              1 |                136 |   94711 | 2015-07-09 13:21:25 | 2015-07-09 13:21:25 |
| 153 | Lexicography          | lexicography          | mw     |              1 |                136 |   94711 | 2015-07-09 13:21:35 | 2015-07-09 13:21:35 |
| 154 | Quotable Quotes       | quotable-quotes       | mw     |              1 |                136 |   94711 | 2015-07-09 13:21:48 | 2015-07-09 13:21:48 |
| 155 | Book Excerpts         | book-excerpts         | mw     |              1 |                136 |   94711 | 2015-07-09 13:21:54 | 2015-07-09 13:21:54 |
| 156 | Religion              | religion              | mw     |              1 |                136 |   94711 | 2015-07-09 13:22:04 | 2015-07-09 13:22:04 |
| 157 | Lifestyle             | lifestyle             | mw     |              1 |                136 |   94711 | 2015-07-09 13:22:10 | 2015-07-09 13:22:10 |
| 158 | Literature            | literature            | mw     |              1 |                136 |   94711 | 2015-07-09 13:22:15 | 2015-07-09 13:22:15 |
| 159 | Online Culture        | online-culture        | mw     |              1 |                136 |   94711 | 2015-07-09 13:22:21 | 2015-07-09 13:22:21 |
| 160 | Topic                 | topic                 | mw     |              0 |               NULL |   94711 | 2015-07-09 13:22:35 | 2015-07-09 13:22:35 |
| 161 | Arts & Entertainment  | arts--entertainment   | mw     |              1 |                160 |   94711 | 2015-07-09 13:22:43 | 2015-07-09 13:22:43 |
| 162 | Science & Technology  | science--technology   | mw     |              1 |                160 |   94711 | 2015-07-09 13:25:46 | 2015-07-09 13:25:46 |
| 163 | Home & Garden         | home--garden          | mw     |              1 |                160 |   94711 | 2015-07-09 13:25:52 | 2015-07-09 13:25:52 |
| 164 | Business              | business              | mw     |              1 |                160 |   94711 | 2015-07-09 13:25:57 | 2015-07-09 13:25:57 |
| 165 | Education & Research  | education--research   | mw     |              1 |                160 |   94711 | 2015-07-09 13:26:04 | 2015-07-09 13:26:04 |
| 166 | Sports & Recreation   | sports--recreation    | mw     |              1 |                160 |   94711 | 2015-07-09 13:26:11 | 2015-07-09 13:26:11 |
| 167 | Health & Fitness      | health--fitness       | mw     |              1 |                160 |   94711 | 2015-07-09 13:26:17 | 2015-07-09 13:26:17 |
| 168 | History & Literature  | history--literature   | mw     |              1 |                160 |   94711 | 2015-07-09 13:26:26 | 2015-07-09 13:26:26 |
| 169 | Medical               | medical               | mw     |              1 |                160 |   94711 | 2015-07-09 13:26:31 | 2015-07-09 13:26:31 |
| 170 | Government            | government            | mw     |              1 |                160 |   94711 | 2015-07-09 13:26:36 | 2015-07-09 13:26:36 |
| 171 | Word of the Year      | word-of-the-year      | mw     |              0 |               NULL |   94711 | 2015-07-09 13:26:45 | 2015-07-09 13:26:45 |
| 172 | Word of the Year 2013 | word-of-the-year-2013 | mw     |              1 |                171 |   94711 | 2015-07-09 13:26:50 | 2015-07-09 13:26:50 |
| 173 | Word of the Year 2014 | word-of-the-year-2014 | mw     |              1 |                171 |   94711 | 2015-07-09 13:26:57 | 2015-07-09 13:26:57 |
| 174 | Word of the Year 2015 | word-of-the-year-2015 | mw     |              1 |                171 |   94711 | 2015-07-09 13:27:03 | 2015-07-09 13:27:03 |
| 176 | Travel                | travel                | mw     |              1 |                160 |   94711 | 2015-07-10 13:31:05 | 2015-07-10 13:31:05 |
| 177 | Test                  | test                  | mw     |              0 |                  0 |   94706 | 2015-07-16 20:03:19 | 2015-07-16 20:03:19 |
+-----+-----------------------+-----------------------+--------+----------------+--------------------+---------+---------------------+---------------------+
53 rows in set (0.00 sec)

^ if I join a table on itself - why in the world would I get different results?

To expand on @MatRichardson said above; basically

  • the first query is getting all nodes with any parents if they have them
  • the second is getting all nodes with any children if they have them

The reason is that a LEFT JOIN selects all rows from the "left" of the join statement and any rows from the right statement that satisfies the join condition (ON clause), and RIGHT JOIN does the exact opposite -> it selects all rows from the right and any rows from the left that satisfies the condition.

In your case, on the second example your selecting the first row (id=84) in the "b" alias, and then since no "a" row matches it, all "a" columns are NULL. In your first example, that row (where b.id=84) is omitted because it doesn't satisfy the condition of a.id=b.parent_taxonomy_id for any "a" row

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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