Trouble writing a query

This is a discussion on Trouble writing a query within the MySQL Database forums, part of the Database Forums category; Hi, I'm using MySQL 5.0. I have three tables ... MODULES (lists available content for the page, e.g. ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-20-2007
laredotornado@zipmail.com
 
Posts: n/a
Default Trouble writing a query

Hi,

I'm using MySQL 5.0. I have three tables ...

MODULES (lists available content for the page, e.g. Gmail, ESPN, CNN)
----------------
ID
NAME

TABS (lists names of groups of modules)
---------
ID
NAME

TAB_MODULES (lists modules that go in each group)
-----------------------
ID
MODULE_ID (foreign key to MODULES.ID column)
TAB_ID (foreign key to TABS.ID column)

I want to write a query that tells me whether a module is displayed on
a tab, given a tab's ID. The result set should always contain the
same number of rows as SELECT COUNT(*) FROM MODULES. I would like it
to contain

MODULE_NAME (name of module)
SELECTED (either 1 or 0)

How do I do this?

Thanks, - Dave

Reply With Quote
  #2 (permalink)  
Old 04-23-2007
Captain Paralytic
 
Posts: n/a
Default Re: Trouble writing a query

On 20 Apr, 17:27, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.com> wrote:
> Hi,
>
> I'm using MySQL 5.0. I have three tables ...
>
> MODULES (lists available content for the page, e.g. Gmail, ESPN, CNN)
> ----------------
> ID
> NAME
>
> TABS (lists names of groups of modules)
> ---------
> ID
> NAME
>
> TAB_MODULES (lists modules that go in each group)
> -----------------------
> ID
> MODULE_ID (foreign key to MODULES.ID column)
> TAB_ID (foreign key to TABS.ID column)
>
> I want to write a query that tells me whether a module is displayed on
> a tab, given a tab's ID. The result set should always contain the
> same number of rows as SELECT COUNT(*) FROM MODULES. I would like it
> to contain
>
> MODULE_NAME (name of module)
> SELECTED (either 1 or 0)
>
> How do I do this?
>
> Thanks, - Dave


Well I think you want:

SELECT
`m`.`NAME` `MODULE_NAME`,
IF(`t`.`MODULE_ID`,1,0)
FROM `MODULES` `m`
LEFT JOIN `TAB_MODULES` `t` ON `m`.`ID` = `t`.`MODULE_ID` AND
`t`.`TAB_ID` = (the id of the tab you are intrrested in)

Reply With Quote
  #3 (permalink)  
Old 04-23-2007
Captain Paralytic
 
Posts: n/a
Default Re: Trouble writing a query

On 23 Apr, 13:09, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 20 Apr, 17:27, "laredotorn...@zipmail.com"
>
>
>
>
>
> <laredotorn...@zipmail.com> wrote:
> > Hi,

>
> > I'm using MySQL 5.0. I have three tables ...

>
> > MODULES (lists available content for the page, e.g. Gmail, ESPN, CNN)
> > ----------------
> > ID
> > NAME

>
> > TABS (lists names of groups of modules)
> > ---------
> > ID
> > NAME

>
> >TAB_MODULES(lists modules that go in each group)
> > -----------------------
> > ID
> > MODULE_ID (foreign key to MODULES.ID column)
> > TAB_ID (foreign key to TABS.ID column)

>
> > I want to write a query that tells me whether a module is displayed on
> > a tab, given a tab's ID. The result set should always contain the
> > same number of rows as SELECT COUNT(*) FROM MODULES. I would like it
> > to contain

>
> > MODULE_NAME (name of module)
> > SELECTED (either 1 or 0)

>
> > How do I do this?

>
> > Thanks, - Dave

>
> Well I think you want:
>
> SELECT
> `m`.`NAME` `MODULE_NAME`,
> IF(`t`.`MODULE_ID`,1,0)
> FROM `MODULES` `m`
> LEFT JOIN `TAB_MODULES` `t` ON `m`.`ID` = `t`.`MODULE_ID` AND
> `t`.`TAB_ID` = (the id of the tab you are intrrested in)- Hide quoted text -
>
> - Show quoted text -


Forgot a bit:
SELECT
`m`.`NAME` `MODULE_NAME`,
IF(`t`.`MODULE_ID`,1,0) `SELECTED`
FROM `MODULES` `m`
LEFT JOIN `TAB_MODULES` `t` ON `m`.`ID` = `t`.`MODULE_ID` AND
`t`.`TAB_ID` = (the id of the tab you are interested in)



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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



All times are GMT +1. The time now is 08:40 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0