使用一个查询收集四个相关表的数据

时间:2017-05-29 21:23:45

标签: mysql sql eloquent

我的数据库中有四个表,categoriespoststagspost_tags

categories具有一对多关系,tagsposts表有多对多的关系。

下面的代码段

显示了所提到的表的describe命令的输出。

mysql> describe posts;
+---------------+--------------+------+-----+---------+-------+
| Field         | Type         | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| id            | int(11)      | NO   | PRI | NULL    |       |
| guid          | varchar(100) | NO   |     | NULL    |       |
| thumbnail_url | varchar(255) | YES  |     | NULL    |       |
| title         | varchar(255) | NO   |     | NULL    |       |
| content       | text         | YES  |     | NULL    |       |
| date          | varchar(50)  | YES  |     | NULL    |       |
| category_id   | int(11)      | YES  | MUL | NULL    |       |
+---------------+--------------+------+-----+---------+-------+
7 rows in set (0,00 sec)

mysql> describe categories;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0,00 sec)

mysql> describe tags;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(30) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0,00 sec)

mysql> describe post_tags;
+---------+---------+------+-----+---------+-------+
| Field   | Type    | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| post_id | int(11) | NO   | PRI | NULL    |       |
| tag_id  | int(11) | NO   | PRI | NULL    |       |
+---------+---------+------+-----+---------+-------+
2 rows in set (0,00 sec)

我将以此格式为帖子创建json Feed。

[
  {
   "id": 1, 
   "title":"Foo",
   "category": [{"id": 1, "name": "Bar"}],
   "tags": [
      {"id": 1, "name": "foo"},
      {"id": 2, "name": "bar"}, 
   ]
   ...
   },

  {
   "id": 2, 
   "title":"Bar",
   "category": [{"id": 3, "name": "Foo"}],
   "tags": [
      {"id": 2, "name": "Tux"},
      {"id": 4, "name": "Quz"}, 
   ]
   ...
   }
]  

我知道我可以通过合并两个查询来实现这个结果,例如

mysql> select categories.*, 
    posts.id,  
    posts.title
from posts 
join categories 
where posts.category_id = categories.id;
+-----+-----------+------+-----------------------------------------------------------------------------------------+
| id  | name      | id   | title                                                                                   |
+-----+-----------+------+-----------------------------------------------------------------------------------------+
|   3 | Business  | 8070 | Demand for Marijuana 10 Times More than that for Ice Cream                              |
|   4 | Celebrity | 8067 | 10 Classic Quotes on Weed                                                               |
|   8 | Law       | 8056 | Proposed Rejig of California Medical Marijuana Laws – What Does it Mean for the User?   |
|  10 | Medical   | 8043 | Cannabis Oil – A Miracle Cure that We’ve Always Known About                             |
|  10 | Medical   | 8052 | Weed May Help Fight Cocaine Addiction                                                   |
|  10 | Medical   | 8073 | Cannabis May Help Treat Epilepsy in Children                                            |
|  15 | Tech      | 8079 | Matchmaking Apps for Weed Lovers a Big Hit                                              |
| 501 | Education | 8059 | The Quick Guide to Pruning Cannabis for More Yield                                      |
| 501 | Education | 8062 | Two Steps to Bring Down Power Costs When Cultivating Weed                               |
| 954 | Pets      | 8049 | Medical Cannabis for Pets – What Every Pet Owner Should Know                            |
+-----+-----------+------+-----------------------------------------------------------------------------------------+

和其他一些查询将相关标签与帖子分组。然后(我使用PHP)使用编程语言来操作结果集并形成最终的数据结构。但我很好奇,有可能用一个查询来实现这个结果吗?

我使用的是Eloquent(不是Laravel),因此也欢迎使用该ORM的解决方案。

P.S。我还有PostCategoryTagPostTag模型。

更新:解决方案ATTEMPT

我为我的查询提出了一个解决方案with patching this one

SELECT categories.*, posts.id, posts.title, tags.* 
FROM posts 
  JOIN categories on categories.id = posts.category_id 
  LEFT OUTER JOIN post_tags on posts.id = post_tags.post_id 
  LEFT OUTER JOIN tags on post_tags.tag_id = tags.id;

给了我,

+-----+-----------+------+-----------------------------------------------------------------------------------------+------+-------------------------+
| id  | name      | id   | title                                                                                   | id   | name                    |
+-----+-----------+------+-----------------------------------------------------------------------------------------+------+-------------------------+
|   3 | Business  | 8070 | Demand for Marijuana 10 Times More than that for Ice Cream                              |   61 | BUSINESS                |
|   3 | Business  | 8070 | Demand for Marijuana 10 Times More than that for Ice Cream                              |  401 | Cannabis                |
|   3 | Business  | 8070 | Demand for Marijuana 10 Times More than that for Ice Cream                              |  402 | weed                    |
|   3 | Business  | 8070 | Demand for Marijuana 10 Times More than that for Ice Cream                              |  414 | Medical Marijuana       |
|   3 | Business  | 8070 | Demand for Marijuana 10 Times More than that for Ice Cream                              |  416 | Marijuana               |
|   3 | Business  | 8070 | Demand for Marijuana 10 Times More than that for Ice Cream                              |  441 | mcig                    |
|   3 | Business  | 8070 | Demand for Marijuana 10 Times More than that for Ice Cream                              |  484 | 420 friendly            |
|   3 | Business  | 8070 | Demand for Marijuana 10 Times More than that for Ice Cream                              |  604 | 420 cloud               |
|   3 | Business  | 8070 | Demand for Marijuana 10 Times More than that for Ice Cream                              |  607 | Medical Cannabis        |
|   3 | Business  | 8070 | Demand for Marijuana 10 Times More than that for Ice Cream                              |  721 | Medical                 |
|   3 | Business  | 8070 | Demand for Marijuana 10 Times More than that for Ice Cream                              | 1149 | Demand for Marijuana    |
|   4 | Celebrity | 8067 | 10 Classic Quotes on Weed                                                               |   69 | CELEBRITY               |
|   4 | Celebrity | 8067 | 10 Classic Quotes on Weed                                                               |  401 | Cannabis                |
|   4 | Celebrity | 8067 | 10 Classic Quotes on Weed                                                               |  402 | weed                    | 

.....

这对我来说似乎没问题。但是当将结果打印到浏览器窗口时,我看到了2个问题。

结果按标签分组(我可以通过某些数组方法除外)。

结果集上不显示帖子ID。

0 个答案:

没有答案