skip to Main Content

I’m trying to get more values in a column and I’m testing it in phpMyAdmin:

I have a table Nieuws that contains in this case ID and a Title.

Table Nieuws
enter image description here

I have a table called Nieuws_tags where I give multiple tags to a item in column Nieuws.

table Nieuws_tags

enter image description here

The final column is called Tags that contains ID and Beschrijving

Table Tags

enter image description here

Here is what I currently have:

SELECT * FROM (`Nieuws`) JOIN `Tags` ON `Tags`.`ID` = `Nieuws`.`ID` 
WHERE `Tags`.`ID` = 1

enter image description here

1 | Titel 1 | 1 | QBNL

Right now I only get one value back QBNL, but I gave it two values QBNL and QBBC. I’m pretty sure that my code is wrong but hopefully someone can help me.

EDIT: What I want to achieve:

enter image description here

2

Answers


  1. You need to bring in junction table Nieuws_tags :

    SELECT * 
    FROM `Nieuws`
    INNER JOIN `Nieuws_tags` ON `Nieuws_tags`.`ID-Nieuws` = `Nieuws`.`ID` 
    INNER JOIN `Tags` ON `Tags`.`ID` = `Nieuws_tags`.`ID-tags` 
    WHERE `Tags`.`ID` = 1
    
    Login or Signup to reply.
  2. Below would be the query if you need all the entries from tags.

    SELECT * 
    FROM Nieuws t1
    INNER JOIN Nieuws_tags t2 ON t2.ID-Nieuws = t1.ID 
    INNER JOIN Tags t3 ON t3.ID = t2.ID-tags 
    WHERE t3.ID-Nieuws = 1
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search