Jump to content

Help creating recordset


barney0o0

Recommended Posts

Hi folks...sorry its been i while since ive done a  database...so please be patient!!

 

Tables are;

 

events

eventsdates - will contain various dates and times of an event, the eventdates.eventid is primary and refers to events.id via foreign key

producer

eventproducer- primarykeys being eventid and producerid, both are foreign keys and refer to event and producer tables.

 

What i cant do is pull them all together to form a recordset, so that i can retrieve relative information on one event, such as all the info in its table, plus the date/s, and producer/s.....everything i try turns out to be complete nonsense!

 

Notes:Each event can have one or more producer. Therefore, the producer contains all the info whereas the eventproducer table will bring it all together. Its all separate as producer/s vary between 1 - 5 . So for example an event with the id of 1 will be listed in the 'eventproducer.eventid' x times and the 'producerid' will be linked and pull together the full info of the producer table.

 

The table structures as as follows;

 

CREATE TABLE `eventdates` (\n  `eventid` int(11) NOT NULL,
  `eventdate` datetime NOT NULL,
`price` decimal(5,2) NOT NULL,
PRIMARY KEY  (`eventid`,`eventdate`),
CONSTRAINT `eventdates_ibfk_1` FOREIGN KEY (`eventid`) REFERENCES `events` (`id`)
ENGINE=InnoDB DEFAULT CHARSET=latin1

CREATE TABLE `events` (\n  `id` int(11) NOT NULL auto_increment,
  `title` varchar(150) NOT NULL,
`imageS` varchar(200) default NULL,
`imageL` varchar(200) default NULL,
`cat` varchar(50) NOT NULL,\n  `cheapchips` varchar(20) default NULL,
  `text` text NOT NULL,
`reco1` varchar(50) default NULL,
`reco2` varchar(50) default NULL,
PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

CREATE TABLE `producer` (\n  `producerID` int(11) NOT NULL auto_increment,
  `company` varchar(50) NOT NULL,
  `text` text NOT NULL,\n  `weblink1` varchar(50) NOT NULL,
  `weblink2` varchar(50) NOT NULL,
  `contactnm` varchar(50) NOT NULL,
  `contacttel` varchar(50) NOT NULL,
  PRIMARY KEY  (`producerID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

CREATE TABLE eventproducers
( eventid INTEGER NOT NULL
, producerid INTEGER NOT NULL
, PRIMARY KEY ( eventid , producerid )
, FOREIGN KEY ( eventid ) REFERENCES events ( id )
, FOREIGN KEY ( producerid ) REFERENCES producers ( producerid )
) ENGINE=InnoDB

 

i hope that someone can help as im loosing the will to live.......similary i hope you can understand what i want to do!

 

Thanks in advance

 

Link to comment
Share on other sites

Thanks Barand

 

Ive had a look before, but im a right mess with trying to bring togther the dates for the particular event.

 

What ive got at the moment is;

 

SELECT events.id, events.title, events.imageS, events.imageL, events.cat, 
events.cheapchips, events.text, events.reco1, events.reco2, 
eventproducers.eventid, eventproducers.producerid,  eventdates.eventid, 
eventdates.eventdate, eventdates.price

FROM events LEFT JOIN eventproducers ON  eventproducers.eventid = events.id  
LEFT JOIN eventdates ON eventdates.eventid = events.id

 

 

....am i completly going in the wrong direction?

 

Many thanks

Link to comment
Share on other sites

No point in selecting the event id from eventdates and eventproducers, they will be same value as events.id.

 

Normal join to get matching records is an INNER JOIN. Use left join if you want events with no dates or no producers

 

Note when joining a structure like this - if an event has 3 dates and 2 producers you get 6 rows returned ( as with event 1 in my sample data below)

SELECT e.id as Event, e.title,
      ed.eventdate, ed.price,
      p.company
FROM events e
      INNER JOIN eventdates ed ON ed.eventid = e.id
      INNER JOIN eventproducers ep ON e.id = ep.eventid
      INNER JOIN producer p ON ep.producerid = p.producerID
ORDER BY e.title, ed.eventdate

->

[pre]

1, 'County show', '2007-09-06 00:00:00', '15.00', 'Production Co B'

1, 'County show', '2007-09-06 00:00:00', '15.00', 'Production Co A'

1, 'County show', '2007-09-07 00:00:00', '12.00', 'Production Co B'

1, 'County show', '2007-09-07 00:00:00', '12.00', 'Production Co A'

3, 'Jazz Fest',  '2007-09-20 00:00:00', '10.00', 'Production Co B'

2, 'Pop concert', '2007-09-13 00:00:00', ' 5.00', 'Production Co A'

Link to comment
Share on other sites

Thanks Barand

 

Your example makes some sence...(believe me i looked as some scripts that i cant make head nor tail of!)

 

Im a little unsure how you form the 'e', 'p' values..and later refre to the 'ep'....sorry, could you give me a little explanation so that i can understand fully

 

many thanks for your help

Link to comment
Share on other sites

e and ed are aliases for the events and eventdates tables

 

SELECT e.id as Event, e.title,

      ed.eventdate, ed.price ...

FROM events e

      INNER JOIN eventdates ed ON ed.eventid = e.id ....

 

Sometimes they are necessary, in this case it's just easier than repeating the tablenames

Link to comment
Share on other sites

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.