跪求一句sql语句,坐等。
4个表,
box是盒子,
note是消息,
box_user是哪些用户在哪些盒子里聊天。
box_note是哪些消息在哪些盒子里(消息可在多盒共享)。
盒子就和微信的一个聊天框一样,消息就是里面的消息。
sql codemysql> 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获取该user的所有盒子以及每个盒子最后一条消息的时间。
我准备了这两个基本sql语句,
sql code//获取所有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;
它们分别执行结果:
sql codemysql> 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)