Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The comma separated list of tables in your FROM clause functions like an inner join, so your query is selecting all of the rows in the tagmaps table and the tags table that have the same tag ID, and of those rows, all of the rows from the books table and the tagmaps table that have the same book ID. The HAVING clause then requires that two rows be returned from that result set with the same book ID. There can only be one row in the books table with any given book ID (assuming book ID is the primary key of the books table), so this condition is never met.</p> <p>What you want is a join without the books table. You are looking for the same book ID appearing twice in the results of the OR clauses (I believe), so you don't want to join the books table with those results because that will ensure you can never have the same book ID in the results more than once.</p> <p>Edit: conceptually, you are essentially combining two different things. You are looking for tags and tagmaps for the same book, and you are also getting the book info from each of those books. So you are actually pulling duplicate other_info data for every instance of the same book ID in the tagmaps table, and then using the distinct clause to reduce that duplicate data down to one row, because all you want is the book ID and other_info. I would consider using two queries or a subquery to do this. There may be other [better] ways as well. I'd have to play around with it to figure it out.</p> <p>For starters, try</p> <pre><code>SELECT DISTINCT tm.book_id, b.other_info FROM tagmap tm inner join tags t on tm.tag_id = t.tag_id left join books b on tm.book_id = b.book_id HAVING count(tm.book_id) = 2 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload