我有一个内容表,可以有与每行关联的注释和标记的不同结果长度。我不确定如何一举得到所有这些信息。
我可以轻松地进行查询以获取内容信息(id)并查询每个表中的注释和标签,但这似乎并不像是有更好的方法。
任何提示或建议? 提前谢谢!
托马斯
我的查询远远看起来像:
SELECT c.*, GROUP_CONCAT(tagWords.tagWord SEPARATOR ', ') AS tags FROM platform.contents c
LEFT OUTER JOIN platform.contentTags ON contentTags.contentId = c.contentId
LEFT OUTER JOIN platform.tagWords ON contentTags.tagId = tagWords.tagId
WHERE c.contentType = 'album' LIMIT 10
GROUP_CONCAT很棒,除了我需要每个表中的字段,因为表格会增长。 :/
我的表格布局:
mysql> explain contentComments;
+--------------+---------------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+-------------------+-------+
| contentId | bigint(19) unsigned | NO | MUL | NULL | |
| userId | bigint(19) unsigned | NO | | NULL | |
| message | varchar(255) | YES | | NULL | |
| stampCreated | timestamp | NO | | CURRENT_TIMESTAMP | |
+--------------+---------------------+------+-----+-------------------+-------+
4 rows in set (0.00 sec)
mysql> explain contentTags;
+-----------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------+------+-----+---------+-------+
| contentId | bigint(20) | NO | MUL | NULL | |
| tagId | bigint(20) | YES | | NULL | |
+-----------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> explain contents;
+------------------+-----------------------------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-----------------------------------------+------+-----+-------------------+----------------+
| contentId | bigint(20) | NO | PRI | NULL | auto_increment |
| contentType | enum('video','album','blogpost','news') | NO | | NULL | |
| userId | bigint(19) unsigned | NO | MUL | NULL | |
| contentTitle | varchar(45) | YES | | NULL | |
| contentDesc | varchar(255) | YES | | NULL | |
+------------------+-----------------------------------------+------+-----+-------------------+----------------+
mysql> explain tagWords;
+---------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------------+------+-----+---------+----------------+
| tagId | bigint(19) unsigned | NO | PRI | NULL | auto_increment |
| tagWord | varchar(45) | YES | | NULL | |
+---------+---------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
答案 0 :(得分:0)
看起来对我来说,虽然我认为你还需要一个group_concat来评论。可能是你的大脑对数据库中N到N关系的想法感到不舒服,考虑到它看起来像你做得对,这会很奇怪:p