/    Sign up×
Community /Pin to ProfileBookmark

How to select from intermediate table while also selecting from parent table.

I have the following tables inside my database:

“`
Table – mytags

| tag_id | tag_name |
| 1 | home |
| 2 | work |

TABLE – mytasks

| task_id | task_name |
| 14 | do my the dishes |
| 12 | Make presentation |

TABLE – mytasks_mytags

| mytag_id | mytask_id |
| 1 | 14 |
| 2 | 14 |
“`

Now i would like to select the mytasks.task_id and mytasks.task_name and then i would like to retrive all the tag names with which this task is being associated with , this i can get from the mytasks_mytags table. Can somebody please guide me on how to go about doing this. ?

to post a comment

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 14.2019 — Use a join -- either inner join if you only want tasks that have tags, or left join if you want tasks even if they don't have tags.
<i>
</i>select mytasks.task_name, mytags.tag_name
from mytasks
inner join mytasks_mytags as mm on mm.task_id = mytasks,task_id
inner join mytags on mm.tag_id = mytags.tag_id
-- where clause her if needed
order by mytasks.task_name -- or whatever makes sense
Copy linkTweet thisAlerts:
@gautamz07authorFeb 14.2019 — @NogDog#1601006 thanks for taking the time to type this out , my heads spinning but will try and make sense of this. TY.
Copy linkTweet thisAlerts:
@gautamz07authorFeb 15.2019 — Came up with the following query with the help of everyone and it seems to work for me::-

``<i>
</i>SELECT mytasks.id , mytasks.task , GROUP_CONCAT( mytags.tag ) AS tags
FROM mytasks_mytags
INNER JOIN mytasks ON mytasks_mytags.mytasks_id = mytasks.id
INNER JOIN mytags ON mytasks_mytags.mytags_id = mytags.tag_id
GROUP BY mytasks.id , mytasks.task<i>
</i>
``

just not sure why i need two attributes supplied to GROUP BY I.E. mytasks.id , mytasks.task
Copy linkTweet thisAlerts:
@NogDogFeb 15.2019 — > @gautamz07#1601038 just not sure why i need two attributes supplied to GROUP BY I.E. mytasks.id , mytasks.task

Because you're using a grouping function (and the same would hold true with a COUNT() function), you need to tell it to group identical (non-grouped) column results, so that it knows what it's grouping on. Did that make any sense?
×

Success!

Help @gautamz07 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...