Jump to content

Recommended Posts

I have a multiple table database with tables that are

Admin Table (Primary key AdminID)

Events Table (Primary Key EventID links to Admin via Creator/Modifier ID and to  Pics by EventID)

Pictures Table (Primary Key PicID links to Events via EventID)

 

and I have a query that is

Select Events.EventID as Event_ID , Events.EventName as Name , Events.Start_Date as Start_date , Events.End_Date as End_date , 

Events.ModifiedDate as Date_mod , Events.CreatedDate as Date_created , Events.Summary as Summary , Admins.Email as Creator , 

COUNT(Pictures.PicID) as Pic_Count from `Events`, `Admins`, `Pictures` Where Events.Start_Date >= '2008-01-19 15:05:23' and Events.Active = '1' 

and Pictures.EventID = Events.EventID and Admins.AdminID = Events.CreatorID Group By Events.EventID Order By Start_Date DESC

To basically splash out a list of all events that started/will start in the past 60 days to never ending.

When I added the part about

COUNT(PICTURES.PicID) as Pic_Count 

and

Pictures.EventID = Events.EventID

the set returned was emptied till I populated pictures table with rows that had EventIDs of events.

 

Is there a way to do this with out using a JOIN to get the Pic COUNT and not have to restrict it to Pictures.EventID = Events.EventID???

 

I assumed this was the easiest way to restrict the COUNT to be only relative to that rows data and not the whole table's count.  I'd prefer not to have to place a dummy row in there to place hold each time a new event is added.

from `Events`, `Admins`, `Pictures` Where Events.Start_Date >= '2008-01-19 15:05:23' and Events.Active = '1' 
and Pictures.EventID = Events.EventID and Admins.AdminID = Events.CreatorID Group By Events.EventID Order By Start_Date DESC

becomes:

from `Events` 
left join `Admins` on ( Admins.AdminID = Events.CreatorID )
left join `Pictures` on ( Pictures.EventID = Events.EventID  )
Where Events.Start_Date >= '2008-01-19 15:05:23' and Events.Active = '1' 
Group By Events.EventID Order By Start_Date DESC

Perfect I was just going to ask the structure cause I got a funny error.

 

Is it okay to use the same textkey EventID in both tables or is that not mysql acceptable.

 

I tried it on an empty/filled pictures table and it returned what I expected.

The primary key of Events is EventID

In the table pictures I use the field called EventID (i.e Pictures.EventID) to link the pics to their event in the table i.e (Events.EventID)

is it okay practice to use the same text for the key's description in 2 different tables when 1 isn't primary and one is linking?

One is primary, and the other is the FK... that's exactly what you want.  In fact, you can replace

 

left join `Pictures` on ( Pictures.EventID = Events.EventID  )

 

with

 

left join `Pictures` using ( EventID)

 

for that very reason.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.