1

結合された2つのテーブルがあります:

table_diagram

book_types_purchasedですENUM('paperback','hardcover')。_ このタイプの行を返したい:

book_title | paperback_date_purchased | hardcover_date_purchased

MySQLでこれを行う方法はありますか?


アップデート

代わりにテーブル名と左結合を一致させた以下の回答を次に示します。

SELECT title,
  paperback.date_purchased AS paperback_date_purchased,
  hardcover.date_purchased AS hardcover_date_purchased

FROM book_projects

LEFT JOIN book_types_purchased AS paperback 
  ON paperback.book_project_id = book_projects.id
  AND paperback.type = 'paperback'

LEFT JOIN book_types_purchased AS hardcover 
  ON hardcover.book_project_id = book_projects.id
  AND hardcover.type = 'hardcover'
4

1 に答える 1

4

テーブルを2回できJOINます。book_types_purchased各タイプに 1 つずつ。

SELECT title,
  paperback.date_purchased AS paperback_date_purchased,
  hardcover.date_purchased AS hardcover_date_purchased

FROM book_projects

JOIN date_purchased AS paperback ON paperback.book_project_id = book_projects.id
  AND paperback.type = 'paperback'

JOIN date_purchased AS hardcover ON hardcover.book_project_id = book_projects.id
  AND hardcover.type = 'hardcover'
于 2013-03-15T19:42:08.857 に答える