2

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
  • Most people here want formatted text, not images. Commented Feb 20, 2019 at 9:19
  • Sorry about that. Commented Feb 20, 2019 at 9:51

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you GMB. Right now I'm getting a error: #1054 unknown column 'Nieuws.ID-tags' in on clause.
@ThijsvanRijn : sorry - should be Nieuws_tags.ID-tags
Is it correct that I still get one value back? Only QBNL and not QBNL and QBBC. I'm so sorry this is al new for me.
@ThijsvanRijn : do you want to filter by tag, or by news ?
In your data there is only one news that has tag 1, hence you get a single record with the current query. However there are two tags that relate to news 1, so if you change the WHERE clause to WHERE Nieuws.ID = 1 you will get rwo records.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.