Note that there are some explanatory texts on larger screens.

plurals
  1. POComplex SELECT query in MySQL
    primarykey
    data
    text
    <p>I have the following tables:</p> <pre><code>articles: id, title, content tags: id, tag, tagCategory tags2articles: id, idTag, idArticle categories: id, title, someOtherFields </code></pre> <p>In a page I need to select all the articles that have a number of tags. I'm using this:</p> <pre><code>SELECT SQL_CALC_FOUND_ROWS a.* FROM articles AS a JOIN tags2articles AS ta ON a.id=ta.idArticle JOIN tags AS t ON ta.idTag=t.id WHERE t.id IN (12,13,16) GROUP BY a.id HAVING COUNT(DISTINCT t.id)=3 </code></pre> <p>This will select all the articles that have the tags with the IDs 12,13 and 16 and it works fine. But, the selected articles might also have other tags that might only be specific to one or more of them.</p> <p>And here comes the tricky part: I want to use these tags to make some filters, so I need another query to select all the distinct tags that the articles above have. Something like this:</p> <pre><code>╔═══════╦══════╦═══════════╦════════════════╗ ║ TagID ║ Tag ║ Category ║ SomeOtherField ║ ╠═══════╬══════╬═══════════╬════════════════╣ ║ id1 ║ tag1 ║ category1 ║ field1 ║ ║ id2 ║ tag2 ║ category2 ║ field2 ║ ║ id3 ║ tag3 ║ category1 ║ field1 ║ ║ id4 ║ tag4 ║ category3 ║ field3 ║ ╚═══════╩══════╩═══════════╩════════════════╝ </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.
    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