/    Sign up×
Community /Pin to ProfileBookmark

How do i query 2 tables at the same time

I have the following tables like below:

“`

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 |

“`

Given that i have the id of the mytask table EG. “14” , now i given that i would like to select the mytasks_mytags.mytag_id that are associated with the mytasks_mytags.mytask_id I.E. “14” and then get the mytasks.task_name , how do i merge this inside one
query in mysql ?

to post a comment

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 31.2019 — That's what "joins" are for.
<i>
</i>select mytasks.task_name, mytags.tag_name
from mytasks
inner join mytasks_mytags as mm on mytasks.task_id = mm.mytask_id
inner join mytags on mytags.mytag_id = mm.mytag_id
where mytasks.task_id = 14

If it's possible for there to be no tags associated with a task but you want to retrieve it anyway, then change the "inner join" instances to "left join".
Copy linkTweet thisAlerts:
@SiriusGDJan 31.2019 — If you "normalize" your database correctly you should end up using "joins" a lot.
Copy linkTweet thisAlerts:
@gautamz07authorFeb 13.2019 — Thanks got it ..
Copy linkTweet thisAlerts:
@rootFeb 15.2019 — Also there is a great DEFCON about Databases and how you can have more than one database on the same server and all be independent as well as that, all run independent simultaneous queries.

So it depends on what you mean two different databases, do you mean tables on one database, two tables off two independent databases.

You may want to consider this as the scaled option if what you are working on is going to be a big repository of some kind.
×

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.26,
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,
)...