Mysql查询查找所有评论,喜欢&喜欢用户完成的号码,与所有活动帖子对应的帖子一起上传的文件。
表格结构如下
1. app_post_comments:此表存储由app_post对应的用户完成的注释
2. desc app_post_files:该表存储用户上传的与app_post对应的文件
3. app_post_likes:这个表商店喜欢由app_post对应的用户完成
4. app_posts:这是app_post表。
我正在使用Zend框架和Mysql。
[{'a': 123, 'b': 456}, {'a': 322, 'b': 44454}]
6行(0.00秒)
选项已完成:
mysql> show tables like '%post%';
+----------------------------+
| Tables_in_cleandb (%post%) |
+----------------------------+
| app_post_comments |
| app_post_files |
| app_post_likes |
| app_posts |
| posts |
+----------------------------+
5 rows in set (0.00 sec)
mysql> show tables like '%app_post%';
+--------------------------------+
| Tables_in_cleandb (%app_post%) |
+--------------------------------+
| app_post_comments |
| app_post_files |
| app_post_likes |
| app_posts |
+--------------------------------+
4 rows in set (0.00 sec)
mysql> desc app_post_comments;
+-------------+----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| comment | varchar(10000) | YES | | NULL | |
| app_post_id | int(11) | NO | | NULL | |
| user_id | int(11) | NO | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| user_type | tinyint(4) | YES | | NULL | |
+-------------+----------------+------+-----+---------+----------------+
7 rows in set (0.04 sec)
mysql> desc app_post_files;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| file_name | varchar(100) | NO | | NULL | |
| type | tinyint(4) | NO | | NULL | |
| app_post_id | int(11) | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> desc app_post_likes;
+-------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| app_post_id | int(11) | NO | | NULL | |
| user_id | int(11) | NO | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| user_type | tinyint(4) | YES | | NULL | |
+-------------+------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
mysql> desc app_posts
-> ;
+------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | | NULL | |
| message | text | NO | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| active | tinyint(4) | YES | | NULL | |
+------------+------------+------+-----+---------+----------------+
示例数据:
SELECT ap.id,ap.active active_post ,ap.user_id ui, ap.message, apc.comment, apf.file_name, apf.type
FROM app_posts AS ap
LEFT JOIN app_post_likes AS apl ON ap.id=apl.app_post_id
RIGHT JOIN app_post_comments apc on ap.id = apc.app_post_id
RIGHT JOIN app_post_files as apf on ap.id = apf.app_post_id