We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,464 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Searching and Joining multiple tables

Hi everyone, I would like to ask a question about MySQL. Say, I have a database like this:

database 1:
articles
|- id
|- title
|- description

database 2:
tags
|- id
|- articleID
|- tag

I needed to search from articles.description and tags.tag and while doing so, the searched articles should get the tag from the tags table and the searched tags should get the description from the arcticles.

displaying them as:
title | description | tags

I'm not really sure how to do this yet as I am not an expert when it comes to MySQL. An answer will be greatly appreciated.

TIA

3
Contributors
4
Replies
2 Days
Discussion Span
5 Months Ago
Last Updated
6
Views
Question
Answered
anjerodesu
Newbie Poster
6 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Hello,

You need a join for this but getting all of the tags on the same line could be tricky. Start with something like this:

select articles.title, 
articles.descripton,
tags.tag
from articles
left join tags on articles.id = tages.articleID

This will give you every record from articles (even those without an entry in the tags table) and all matching records from the tags table.

rch1231
Veteran Poster
1,045 posts since Sep 2009
Reputation Points: 142
Solved Threads: 154
Skill Endorsements: 12
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11

I tried it by using:
SELECT articles.* , tags.articleID, tags.tag FROM articles, tags WHERE articles.description LIKE "%sample%" OR articles.title LIKE "%sample%" OR tags.tag LIKE "%sample%" GROUP BY articles.id
but the problem is that the tag is not the right one for the article.

The results are:
title 1 | description 1 | tags 3
title 2 | description 2 | tags 3
title 3 | description 3 | tags 3

Is there a way that it will return the right tag for the right article?

anjerodesu
Newbie Poster
6 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Yeah, I'm a bit of a noob-like stupid with MySQL. Anyway, adding LEFT JOIN solved the problem.

SELECT articles.* , tags.articleID, tags.tag
FROM articles
LEFT JOIN tags
ON articles.id = tags.image_id
WHERE articles.description LIKE "%sample%"
OR articles.title LIKE "%sample%"
OR tags.tag LIKE "%sample%"
GROUP BY articles.id

It may help somebody in the future. :)

anjerodesu
Newbie Poster
6 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 5 Months Ago by rch1231 and adam_k

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0657 seconds using 2.68MB