时间:2021-07-01 10:21:17 帮助过:6人阅读
mysql> describe box; +---------------------------+------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------------------+------------+------+-----+---------+----------------+ | box_id | bigint(20) | NO | PRI | NULL | auto_increment | | type | tinyint(4) | NO | | NULL | | | status_type | char(1) | NO | | NULL | | | create_time | datetime | NO | | NULL | | | delete_time_from_one_part | datetime | NO | | NULL | | +---------------------------+------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec) mysql> describe box_user; +---------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+------------+------+-----+---------+-------+ | user_id | bigint(20) | NO | PRI | 0 | | | box_id | bigint(20) | NO | PRI | 0 | | +---------+------------+------+-----+---------+-------+ 2 rows in set (0.01 sec) mysql> describe note; +-------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+----------------+ | note_id | bigint(20) | NO | PRI | NULL | auto_increment | | user_id | bigint(20) | NO | MUL | NULL | | | type | tinyint(4) | NO | | NULL | | | content | text | NO | | NULL | | | mood | tinyint(4) | NO | | NULL | | | locate | varchar(30) | NO | | none | | | privacy | char(1) | NO | | 1 | | | create_time | datetime | NO | MUL | NULL | | | delay | int(11) | NO | | 0 | | | festival | char(30) | NO | | NULL | | | delete_time | datetime | NO | | NULL | | +-------------+-------------+------+-----+---------+----------------+ 11 rows in set (0.00 sec) mysql> describe box_note; +---------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+------------+------+-----+---------+-------+ | note_id | bigint(20) | NO | PRI | 0 | | | box_id | bigint(20) | NO | PRI | 0 | | +---------+------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
//获取所有user_id用户的盒子 select box.box_id,type,status_type from box,box_user where box.box_id=box_user.box_id and box_user.user_id=1; //获取所有内部具有消息的user_id用户的盒子以及最后更新时间 select box_id,max(create_time) as time from note,box_note where note.note_id=box_note.note_id and note.user_id=1 group by box_id;
mysql> select box.box_id,type,status_type from box,box_user where box.box_id=box_user.box_id and box_user.user_id=1; +--------+------+-------------+ | box_id | type | status_type | +--------+------+-------------+ | 1 | 0 | 0 | | 6 | 1 | 0 | | 7 | 3 | 0 | | 8 | 3 | 0 | +--------+------+-------------+ 4 rows in set (0.00 sec) mysql> select box_id,max(create_time) as time from note,box_note where note.note_id=box_note.note_id and note.user_id=1 group by box_id; +--------+---------------------+ | box_id | time | +--------+---------------------+ | 1 | 2012-05-21 00:00:00 | | 6 | 2012-05-30 00:00:00 | +--------+---------------------+ 2 rows in set (0.00 sec)