Bluehost.com Web Hosting $6.95

Retrieving Data from Multiple Tables

This is a discussion on Retrieving Data from Multiple Tables within the MySQL Database forums, part of the Database Forums category; I have four tables - news, events, photos and team_members which contain different data. Each of the tables has a field ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-28-2007
chris_huh
 
Posts: n/a
Default Retrieving Data from Multiple Tables

I have four tables - news, events, photos and team_members which
contain different data. Each of the tables has a field called 'draft'
allowing users to save an entry as a draft rather than publish it
right away (if it is saved as draft 'yes' is entered into the draft
field). What i want to do is do a COUNT of all items which are saved
as drafts.

So what i need to do is get the data for all four tables and do a
COUNT of rows, but how can i get the data from all four tables. It
doesn't seem to be a JOIN function as that links two corresponding
values, i just want to grab all data from the four tables WHERE draft
= 'yes'.


I have tried:

SELECT local_news.draft, events.draft, photos.draft,
team_members.draft
FROM local_news, events, photos, team_members
WHERE local_news.draft = 'yes' AND events.draft = 'yes' AND
photos.draft = 'yes' AND team_members.draft = 'yes'

but that doesn't work.

Reply With Quote
  #2 (permalink)  
Old 03-28-2007
Sean
 
Posts: n/a
Default Re: Retrieving Data from Multiple Tables


"chris_huh" <chris.huh@gmail.com> wrote in message
news:1175076229.625049.215320@o5g2000hsb.googlegro ups.com...
>I have four tables - news, events, photos and team_members which
> contain different data. Each of the tables has a field called 'draft'
> allowing users to save an entry as a draft rather than publish it
> right away (if it is saved as draft 'yes' is entered into the draft
> field). What i want to do is do a COUNT of all items which are saved
> as drafts.
>
> So what i need to do is get the data for all four tables and do a
> COUNT of rows, but how can i get the data from all four tables. It
> doesn't seem to be a JOIN function as that links two corresponding
> values, i just want to grab all data from the four tables WHERE draft
> = 'yes'.
>
>
> I have tried:
>
> SELECT local_news.draft, events.draft, photos.draft,
> team_members.draft
> FROM local_news, events, photos, team_members
> WHERE local_news.draft = 'yes' AND events.draft = 'yes' AND
> photos.draft = 'yes' AND team_members.draft = 'yes'
>
> but that doesn't work.
>



I am not an expert with MySQL ... but this worked in SQL ...

SELECT
(SELECT count(draft) from local_news WHERE draft = 'yes') AS
DRAFT_LOCAL_NEWS,
(SELECT count(draft) from events WHERE draft = 'yes') AS DRAFT_EVENTS,
(SELECT count(draft) from photos WHERE draft = 'yes') AS DRAFT_PHOTOS
(SELECT count(draft) from team_members WHERE draft = 'yes') AS
DRAFT_TEAM_MEMBERS

Incidentally, I would have had the draft column as a boolean/bit field and
use 1/0 rather than entering the text for "yes" and "no"

Sean



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 07:38 AM.


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