Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-24-2008, 06:09 PM
cruiserweight
 
Posts: n/a
Default table join /query help

I am at the limits of my limited mysql knowledge. I have five tables:
activity, country, category, activity_category, activity_country.

activity is a table of holiday activities, for example "camping in
yosemite" or "rock climbing in moab"; country is a list of countries
(1=usa, 2=mexico, 3=canada, 4=brazil, 5=italy, etc); category is a
list of activity types: camping, diving, rock climbing, cycling, etc.
activity_category is two fields, activity_id and category_id;
activity_country the same; activity_id and country_id.

activities can have more than one country as well as more than one
category (a hiking and cycling trip though italy and france, for
example).

What I am trying to select is activities by country by category, for
example, cycling trips in italy. I assume my tables are set up
correctly? I don't quite understand table joins, and i suspect that
this might be the answer to my problem. if someone could help explain,
or point me in the right direction to learn on my own, i would be
forever grateful. thanks very much in advance for your time and help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-24-2008, 06:09 PM
Captain Paralytic
 
Posts: n/a
Default Re: table join /query help

On 22 Apr, 05:03, cruiserweight <bayo...@yahoo.com> wrote:
> I am at the limits of my limited mysql knowledge. I have five tables:
> activity, country, category, activity_category, activity_country.
>
> activity is a table of holiday activities, for example "camping in
> yosemite" or "rock climbing in moab"; country is a list of countries
> (1=usa, 2=mexico, 3=canada, 4=brazil, 5=italy, etc); category is a
> list of activity types: camping, diving, rock climbing, cycling, etc.
> activity_category is two fields, activity_id and category_id;
> activity_country the same; activity_id and country_id.
>
> activities can have more than one country as well as more than one
> category (a hiking and cycling trip though italy and france, for
> example).
>
> What I am trying to select is activities by country by category, for
> example, cycling trips in italy. I assume my tables are set up
> correctly? I don't quite understand table joins, and i suspect that
> this might be the answer to my problem. if someone could help explain,
> or point me in the right direction to learn on my own, i would be
> forever grateful. thanks very much in advance for your time and help.


I don't see the need for the "activities" table.
country, category, country_category
would seem to serve the same purpose
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-24-2008, 06:09 PM
Rik Wasmus
 
Posts: n/a
Default Re: table join /query help

On Tue, 22 Apr 2008 10:43:22 +0200, Captain Paralytic
<paul_lautman@yahoo.com> wrote:

> On 22 Apr, 05:03, cruiserweight <bayo...@yahoo.com> wrote:
>> I am at the limits of my limited mysql knowledge. I have five tables:
>> activity, country, category, activity_category, activity_country.
>>
>> activity is a table of holiday activities, for example "camping in
>> yosemite" or "rock climbing in moab"; country is a list of countries
>> (1=usa, 2=mexico, 3=canada, 4=brazil, 5=italy, etc); category is a
>> list of activity types: camping, diving, rock climbing, cycling, etc.
>> activity_category is two fields, activity_id and category_id;
>> activity_country the same; activity_id and country_id.
>>
>> activities can have more than one country as well as more than one
>> category (a hiking and cycling trip though italy and france, for
>> example).
>>
>> What I am trying to select is activities by country by category, for
>> example, cycling trips in italy. I assume my tables are set up
>> correctly? I don't quite understand table joins, and i suspect that
>> this might be the answer to my problem. if someone could help explain,
>> or point me in the right direction to learn on my own, i would be
>> forever grateful. thanks very much in advance for your time and help.

>
> I don't see the need for the "activities" table.
> country, category, country_category
> would seem to serve the same purpose


Unless as I suspect activities are an integral part of his data and goal
to maintain. It's not the question wether or not a country has certain
activities, but wether there are certain (scheduled?) activities in that
country involving one of the categories. It could even be possible
activities cross borders and as the OP indicated an activity has several
categories.

For readabilities sake I'd split it into multiple queries (possibly
determenining category.id and country.id beforehand). However, it should
be possible in one query, allthough it seems less efficient:

SELECT
act.id, act.name
FROM countries cn
JOIN activity_country acn
ON acn.country_id = cn.id
JOIN activities act
ON act.id = acn.act_id
JOIN activity_category ac
ON ac.act_id = at.id
JOIN categories cat
ON cat.id = ac.cat_id
AND cat.name LIKE 'cycling'
WHERE cn.name LIKE 'Italy'
GROUP BY act.id


If you know the country.id & activity.id it becomes somewhat simpler:

SELECT act.id, act.name
FROM activity_country acn
JOIN activities act
ON act.id = acn.act_id
JOIN activity_category ac
ON ac.act_id = at.id
AND ac.id = <activity.id>
WHERE acn.country_id = <country.id>
--
Rik Wasmus
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-24-2008, 06:09 PM
cruiserweight
 
Posts: n/a
Default Re: table join /query help

Thanks! I will work with that. I need to get my head around JOIN ...
at the moment i am using this:

SELECT
activity.activity_id, activity_title, activity_photo,
activity_short_desc, activity_category.category_id,
activity_country.country_id
FROM
activity, activity_category, activity_country
WHERE
activity_category.category_id ='5' // or whatever
AND activity_country.country_id = '$country_id'
AND activity.activity_id = activity_country.activity_id
AND activity.activity_id = activity_category.activity_id

At least for the moment it is working. But I'm not sure it's the
correct (or most efficient) way to go about things, and I am having an
impossible time creating similar queries. Is the JOIN method more
efficient? Thanks again.

On Apr 22, 4:15 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Tue, 22 Apr 2008 10:43:22 +0200, Captain Paralytic
>
>
>
> <paul_laut...@yahoo.com> wrote:
> > On 22 Apr, 05:03, cruiserweight <bayo...@yahoo.com> wrote:
> >> I am at the limits of my limited mysql knowledge. I have five tables:
> >> activity, country, category, activity_category, activity_country.

>
> >> activity is a table of holiday activities, for example "camping in
> >> yosemite" or "rock climbing in moab"; country is a list of countries
> >> (1=usa, 2=mexico, 3=canada, 4=brazil, 5=italy, etc); category is a
> >> list of activity types: camping, diving, rock climbing, cycling, etc.
> >> activity_category is two fields, activity_id and category_id;
> >> activity_country the same; activity_id and country_id.

>
> >> activities can have more than one country as well as more than one
> >> category (a hiking and cycling trip though italy and france, for
> >> example).

>
> >> What I am trying to select is activities by country by category, for
> >> example, cycling trips in italy. I assume my tables are set up
> >> correctly? I don't quite understand table joins, and i suspect that
> >> this might be the answer to my problem. if someone could help explain,
> >> or point me in the right direction to learn on my own, i would be
> >> forever grateful. thanks very much in advance for your time and help.

>
> > I don't see the need for the "activities" table.
> > country, category, country_category
> > would seem to serve the same purpose

>
> Unless as I suspect activities are an integral part of his data and goal
> to maintain. It's not the question wether or not a country has certain
> activities, but wether there are certain (scheduled?) activities in that
> country involving one of the categories. It could even be possible
> activities cross borders and as the OP indicated an activity has several
> categories.
>
> For readabilities sake I'd split it into multiple queries (possibly
> determenining category.id and country.id beforehand). However, it should
> be possible in one query, allthough it seems less efficient:
>
> SELECT
> act.id, act.name
> FROM countries cn
> JOIN activity_country acn
> ON acn.country_id = cn.id
> JOIN activities act
> ON act.id = acn.act_id
> JOIN activity_category ac
> ON ac.act_id = at.id
> JOIN categories cat
> ON cat.id = ac.cat_id
> AND cat.name LIKE 'cycling'
> WHERE cn.name LIKE 'Italy'
> GROUP BY act.id
>
> If you know the country.id & activity.id it becomes somewhat simpler:
>
> SELECT act.id, act.name
> FROM activity_country acn
> JOIN activities act
> ON act.id = acn.act_id
> JOIN activity_category ac
> ON ac.act_id = at.id
> AND ac.id = <activity.id>
> WHERE acn.country_id = <country.id>
> --
> Rik Wasmus


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-24-2008, 06:09 PM
Captain Paralytic
 
Posts: n/a
Default Re: table join /query help

On 23 Apr, 02:12, cruiserweight <bayo...@yahoo.com> wrote:
> On Apr 22, 4:15 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
>
> > On Tue, 22 Apr 2008 10:43:22 +0200, Captain Paralytic

>
> > <paul_laut...@yahoo.com> wrote:
> > > On 22 Apr, 05:03, cruiserweight <bayo...@yahoo.com> wrote:
> > >> I am at the limits of my limited mysql knowledge. I have five tables:
> > >> activity, country, category, activity_category, activity_country.

>
> > >> activity is a table of holiday activities, for example "camping in
> > >> yosemite" or "rock climbing in moab"; country is a list of countries
> > >> (1=usa, 2=mexico, 3=canada, 4=brazil, 5=italy, etc); category is a
> > >> list of activity types: camping, diving, rock climbing, cycling, etc.
> > >> activity_category is two fields, activity_id and category_id;
> > >> activity_country the same; activity_id and country_id.

>
> > >> activities can have more than one country as well as more than one
> > >> category (a hiking and cycling trip though italy and france, for
> > >> example).

>
> > >> What I am trying to select is activities by country by category, for
> > >> example, cycling trips in italy. I assume my tables are set up
> > >> correctly? I don't quite understand table joins, and i suspect that
> > >> this might be the answer to my problem. if someone could help explain,
> > >> or point me in the right direction to learn on my own, i would be
> > >> forever grateful. thanks very much in advance for your time and help.

>
> > > I don't see the need for the "activities" table.
> > > country, category, country_category
> > > would seem to serve the same purpose

>
> > Unless as I suspect activities are an integral part of his data and goal
> > to maintain. It's not the question wether or not a country has certain
> > activities, but wether there are certain (scheduled?) activities in that
> > country involving one of the categories. It could even be possible
> > activities cross borders and as the OP indicated an activity has several
> > categories.

>
> > For readabilities sake I'd split it into multiple queries (possibly
> > determenining category.id and country.id beforehand). However, it should
> > be possible in one query, allthough it seems less efficient:

>
> > SELECT
> > act.id, act.name
> > FROM countries cn
> > JOIN activity_country acn
> > ON acn.country_id = cn.id
> > JOIN activities act
> > ON act.id = acn.act_id
> > JOIN activity_category ac
> > ON ac.act_id = at.id
> > JOIN categories cat
> > ON cat.id = ac.cat_id
> > AND cat.name LIKE 'cycling'
> > WHERE cn.name LIKE 'Italy'
> > GROUP BY act.id

>
> > If you know the country.id & activity.id it becomes somewhat simpler:

>
> > SELECT act.id, act.name
> > FROM activity_country acn
> > JOIN activities act
> > ON act.id = acn.act_id
> > JOIN activity_category ac
> > ON ac.act_id = at.id
> > AND ac.id = <activity.id>
> > WHERE acn.country_id = <country.id>
> > --
> > Rik Wasmus

> Thanks! I will work with that. I need to get my head around JOIN ...
> at the moment i am using this:
>
> SELECT
> activity.activity_id, activity_title, activity_photo,
> activity_short_desc, activity_category.category_id,
> activity_country.country_id
> FROM
> activity, activity_category, activity_country
> WHERE
> activity_category.category_id ='5' // or whatever
> AND activity_country.country_id = '$country_id'
> AND activity.activity_id = activity_country.activity_id
> AND activity.activity_id = activity_category.activity_id
>
> At least for the moment it is working. But I'm not sure it's the
> correct (or most efficient) way to go about things, and I am having an
> impossible time creating similar queries. Is the JOIN method more
> efficient? Thanks again.
>

Please do not top post (top posting fixed).
Using explicit JOINs it not necessarily more efficient, but it is
clearer and much less troublesome as implicit comma joins are weighted
differently.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-24-2008, 06:09 PM
cruiserweight
 
Posts: n/a
Default Re: table join /query help

On Apr 23, 5:03 pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 23 Apr, 02:12, cruiserweight <bayo...@yahoo.com> wrote:
>
> > On Apr 22, 4:15 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:

>
> > > On Tue, 22 Apr 2008 10:43:22 +0200, Captain Paralytic

>
> > > <paul_laut...@yahoo.com> wrote:
> > > > On 22 Apr, 05:03, cruiserweight <bayo...@yahoo.com> wrote:
> > > >> I am at the limits of my limited mysql knowledge. I have five tables:
> > > >> activity, country, category, activity_category, activity_country.

>
> > > >> activity is a table of holiday activities, for example "camping in
> > > >> yosemite" or "rock climbing in moab"; country is a list of countries
> > > >> (1=usa, 2=mexico, 3=canada, 4=brazil, 5=italy, etc); category is a
> > > >> list of activity types: camping, diving, rock climbing, cycling, etc.
> > > >> activity_category is two fields, activity_id and category_id;
> > > >> activity_country the same; activity_id and country_id.

>
> > > >> activities can have more than one country as well as more than one
> > > >> category (a hiking and cycling trip though italy and france, for
> > > >> example).

>
> > > >> What I am trying to select is activities by country by category, for
> > > >> example, cycling trips in italy. I assume my tables are set up
> > > >> correctly? I don't quite understand table joins, and i suspect that
> > > >> this might be the answer to my problem. if someone could help explain,
> > > >> or point me in the right direction to learn on my own, i would be
> > > >> forever grateful. thanks very much in advance for your time and help.

>
> > > > I don't see the need for the "activities" table.
> > > > country, category, country_category
> > > > would seem to serve the same purpose

>
> > > Unless as I suspect activities are an integral part of his data and goal
> > > to maintain. It's not the question wether or not a country has certain
> > > activities, but wether there are certain (scheduled?) activities in that
> > > country involving one of the categories. It could even be possible
> > > activities cross borders and as the OP indicated an activity has several
> > > categories.

>
> > > For readabilities sake I'd split it into multiple queries (possibly
> > > determenining category.id and country.id beforehand). However, it should
> > > be possible in one query, allthough it seems less efficient:

>
> > > SELECT
> > > act.id, act.name
> > > FROM countries cn
> > > JOIN activity_country acn
> > > ON acn.country_id = cn.id
> > > JOIN activities act
> > > ON act.id = acn.act_id
> > > JOIN activity_category ac
> > > ON ac.act_id = at.id
> > > JOIN categories cat
> > > ON cat.id = ac.cat_id
> > > AND cat.name LIKE 'cycling'
> > > WHERE cn.name LIKE 'Italy'
> > > GROUP BY act.id

>
> > > If you know the country.id & activity.id it becomes somewhat simpler:

>
> > > SELECT act.id, act.name
> > > FROM activity_country acn
> > > JOIN activities act
> > > ON act.id = acn.act_id
> > > JOIN activity_category ac
> > > ON ac.act_id = at.id
> > > AND ac.id = <activity.id>
> > > WHERE acn.country_id = <country.id>
> > > --
> > > Rik Wasmus

> > Thanks! I will work with that. I need to get my head around JOIN ...
> > at the moment i am using this:

>
> > SELECT
> > activity.activity_id, activity_title, activity_photo,
> > activity_short_desc, activity_category.category_id,
> > activity_country.country_id
> > FROM
> > activity, activity_category, activity_country
> > WHERE
> > activity_category.category_id ='5' // or whatever
> > AND activity_country.country_id = '$country_id'
> > AND activity.activity_id = activity_country.activity_id
> > AND activity.activity_id = activity_category.activity_id

>
> > At least for the moment it is working. But I'm not sure it's the
> > correct (or most efficient) way to go about things, and I am having an
> > impossible time creating similar queries. Is the JOIN method more
> > efficient? Thanks again.

>
> Please do not top post (top posting fixed).
> Using explicit JOINs it not necessarily more efficient, but it is
> clearer and much less troublesome as implicit comma joins are weighted
> differently.


Sorry about top posing. I will study up on joins. Thank you.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 05:13 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145