我有一个带有父级>子级分层数据的分类表。我认为我在下面发布的查询应该返回相同的结果,因为我自己加入了同一个表 - 但是我得到了不同的结果。为什么呢?
这是我的左连接查询,结果是:
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)
^如果我自己加入一张桌子 - 为什么在世界上我会得到不同的结果?
答案 0 :(得分:1)
扩展@MatRichardson上面说的;基本上
答案 1 :(得分:1)
原因是LEFT JOIN选择" left"中的所有行。 join语句和右语句中满足连接条件的任何行(ON子句),RIGHT JOIN完全相反 - >它选择右边的所有行和左边满足条件的所有行。
在您的情况下,在第二个示例中,您在" b"中选择第一行(id = 84)。别名,然后因为没有" a"行匹配它,所有" a"列为NULL。
在您的第一个示例中,省略了该行(其中b.id = 84),因为它不满足a.id=b.parent_taxonomy_id
对任何" a"的条件。行