跪求一句SQL語句,坐等。解決辦法

來源:互聯網
上載者:User
跪求一句SQL語句,坐等。
4個表,

box是盒子,
note是訊息,
box_user是哪些使用者在哪些盒子裡聊天。
box_note是哪些訊息在哪些盒子裡(訊息可在多盒共用)。

盒子就和的一個聊天框一樣,訊息就是裡面的訊息。

SQL code
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擷取該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 code
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) 
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.