mysql - Inner Join subselect as separate column -


i have 3 tables: 1 called "documents" 1 called "tags" , 1 called "documents_tags"

in table "document_tags" have 3 columns: id, document_id, tag_id means via table muliple tags can linked multiple documents.

now want put out datasets of table "tags" , in each result-row want have "1" if tag linked document of id "3" (in table document_tags) , "0" if not.

is possible in 1 single query?

use left join

select t.id, t.title, if(dt.document_id, 0, 1) linked tags t left join documents_tags dt on dt.tag_id = t.id , dt.document_id = $doc_id 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -