2017/01/08
MySQL 内部結合と日付ソート
( ..)φメモメモ
【お題】
historyテーブルをbookテーブルと"history.bookid = book.bookid"を満たすように内部結合し、この表から"userid = 1"の場合の本の ID(bookid), タイトル(title), 作者(author), あらすじ(storyline) を日付の近い順に10冊分取得する
【テーブル】
①history
・履歴ID > historyid integer primary key
・ユーザーID > userid integer
・日付 > date timestamp default current_timestamp
・本ID > bookid integer
②book
・本ID > bookid integer primary key
・タイトル > title varchar(256)
・作者 > author varchar(128)
・あらすじ > storyline text
最新10件は、日付降順でソートした後、上の10件までを抽出すればOK
SELECT book.bookid, title, author, storyline
FROM history INNER JOIN book ON history.bookid = book.bookid
WHERE userid = 1 ORDER BY date DESC LIMIT 10;
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿