mysql - LEFT JOIN 显示与 RIGHT JOIN 不同的结果 - 当自己连接表时......为什么?

标签 mysql sql join

我有一个包含父->子分层数据的分类法表。我认为我在下面发布的查询应该返回相同的结果,因为我自己加入了同一个表 - 但我得到了不同的结果。为什么?

这是我的左连接查询及其结果:

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)

```

这是我的正确连接查询及其结果:

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)

这是分类表中的数据:

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)

^ 如果我自己连接一个表 - 为什么我会得到不同的结果?

最佳答案

扩展上面所说的@MatRichardson;基本上

  • 第一个查询是获取所有节点及其父节点
  • 第二个是获取所有有子节点的节点

关于mysql - LEFT JOIN 显示与 RIGHT JOIN 不同的结果 - 当自己连接表时......为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31463977/

相关文章:

php - password_hash, password_verify, MySQL 误解?

mysql - 使用 LEFT JOIN 和 UNION 组合多个表

php - 像 CONCAT 与 REGEXP mysql

sql - 将 Microsoft Access 连接到 Delphi 7 时尝试执行 SQL 查询时提示用户名和密码

mysql - 使用 SQL 从第二个 P 标记 <p> 之后的数据库获取文本

sql - 如何获得 derby db 上 2 个 datetimestamp 之间的小时差?

sql - 如何访问 Rails3 中的连接表属性?

python - 使用 union 比加入 apache spark 更有效,还是没关系?

phpmyadmin token 不匹配

php - mysql查询数组检查