Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL Many-to-Many Relationship Select Conditions
    primarykey
    data
    text
    <p>I have two tables books and tags which are in a many-to-many relationship via the table books_tags. What I want to do is select all books that have one or more tag(s) like '%tag%'. No problem. However, I also have a group_concat field which I want to be filled with all the tags that are related to the selected book.</p> <p>My select query basically looks like this:</p> <pre><code>select flbdb_books.id, flbdb_books.title, flbdb_tags.id, flbdb_tags.name, group_concat(distinct concat('["', flbdb_tags.id, '","', flbdb_tags.name, '"]') separator ',') as tags from flbdb_books join flbdb_books_tags on flbdb_books.id = flbdb_books_tags.book_id join flbdb_tags on flbdb_tags.id = flbdb_books_tags.tag_id group by flbdb_books.id; </code></pre> <p>This delivers the desired result if I don't have any conditions. I use PHP and json_decode to read the tags field. I have tried three different ways to filter the results, but none of them works.</p> <ol> <li><p>use where:</p> <p><code>where flbdb_tags.name like '%poetr%'</code></p> <p>however, this means that the tags field (the one that is assembled using group_concat) is only filled with this tag</p></li> <li><p>use having</p> <p><code>having flbdb_tags.name like '%poetr%'</code></p> <p>this does not return all books tagged poetry, because in some cases the poetry tag is hidden behind another tag due to the group by</p></li> <li><p>use having on the group_concat field</p> <p><code>having tags like '%poetr%'</code></p> <p>this would actually do the trick, but I need to do the case-insensitive searching, and upper(tags) doesn't work</p></li> </ol> <p>I hope it is clear what I want to do.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    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.
    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