that didn't work but i tried doing it a different way, which also makes it easier to manage
I now have 3 tables
creatures
CREATE TABLE IF NOT EXISTS `creatures` (
`ID` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`type` enum('Egg','1','2','3') NOT NULL,
`sdesc` varchar(255) NOT NULL,
`desc` longtext NOT NULL,
`clicks` int(3) NOT NULL,
`family` varchar(255) NOT NULL,
`evolve` int(11) NOT NULL COMMENT 'creature it evolves to',
`evolvealt` int(11) NOT NULL COMMENT 'Alternate evolution',
`chance` int(11) NOT NULL COMMENT 'Chance (Percentage) of getting the alternate',
`rarity` int(1) NOT NULL COMMENT '1 = Common | 2 = Uncommon | 3 = Rare | 4 = Ultra-Rare',
`planet` enum('Calona','Herosa','Viania') NOT NULL,
`url` varchar(255) NOT NULL,
PRIMARY KEY (`ID`),
KEY `evolvealt` (`evolvealt`),
KEY `chance` (`chance`),
KEY `family` (`family`,`planet`,`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=77 ;
--
-- Dumping data for table `creatures`
--
INSERT INTO `creatures` (`ID`, `name`, `type`, `sdesc`, `desc`, `clicks`, `family`, `evolve`, `evolvealt`, `chance`, `rarity`, `planet`, `url`) VALUES
(1, 'Egg', 'Egg', 'A cool and glossy bluish egg', 'These eggs are found in forests and woodland areas, normally in cooler air where snow falls. Their tough but glossy shell makes them well adapted to the colder environment; serving as camouflage against the snow and helping prevent it being eaten by predators.', 10, 'Hawn', 21, 21, 0, 2, 'Calona', 'http://calona.net/images/hawn-egg.png'),
(2, 'Egg', 'Egg', 'A strange bat like egg', 'Although Jone eggs have bat like wings, they can not fly. In cold nights the wings wrap themselves around the egg, keeping themselves warm, as well as acting as a defence against small creatures', 12, 'Jone', 16, 16, 0, 1, 'Calona', 'http://calona.net/images/jone-egg.png'),
(3, 'Egg', 'Egg', 'The egg is comfortably warm... and purring?', 'This brightly pink egg is a favourite amongst people, especially young children; who love the natural warmth it gives off and the gentle purr it often makes. They find it comforting and relaxing.', 9, 'Kitt', 14, 14, 0, 1, 'Calona', 'http://calona.net/images/kitt-egg.png'),
(4, 'Egg', 'Egg', 'An egg thats been partially eaten', 'It looks as though part of this egg has been eaten. The creature inside is slowly eating its way out. Mite eggs are often use as a source of food for many other creatures since they taste very much like cheese', 10, 'Mite', 18, 18, 0, 2, 'Calona', 'http://calona.net/images/mite_egg.png'),
(5, 'Egg', 'Egg', 'This egg is covered with a layer of slime', 'This egg is usually found in swampy areas. It is protected by a think layer of disgusting tasting and smelling slime. Because of this there are too many of these eggs about', 15, 'Swa', 20, 20, 0, 1, 'Calona', 'http://calona.net/images/swa_egg.png'),
(6, 'Egg', 'Egg', 'A floating egg with gullies', 'This egg can only be found near or just under the surface of pure clean water', 14, 'Aqua', 8, 8, 0, 3, 'Calona', 'http://calona.net/images/aqua_egg.png'),
(7, 'Egg', 'Egg', 'This egg is double to size of the others, and the time is wrong', 'Time eggs are the strangest type of egg. Though they have a clock face and hands on the outside of the shell, they are always the wrong time', 10, 'Time', 11, 11, 0, 4, 'Calona', 'http://calona.net/images/time_egg.png'),
creatures_chances
CREATE TABLE IF NOT EXISTS `creatures_chances` (
`ID` int(1) NOT NULL,
`rarity` varchar(10) NOT NULL COMMENT 'The rarity of the creature',
`begin` int(2) NOT NULL,
`end` int(3) NOT NULL,
PRIMARY KEY (`rarity`),
KEY `chance` (`begin`),
KEY `end` (`end`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `creatures_chances`
--
INSERT INTO `creatures_chances` (`ID`, `rarity`, `begin`, `end`) VALUES
(1, 'Common', 0, 60),
(3, 'Rare', 81, 94),
(4, 'Ultra-Rare', 95, 100),
(2, 'Uncommon', 61, 80);
user_access
CREATE TABLE IF NOT EXISTS `user_access` (
`uid` int(11) NOT NULL COMMENT 'User ID',
`cid` int(11) NOT NULL COMMENT 'Creature ID',
PRIMARY KEY (`uid`,`cid`),
KEY `cid` (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `user_access`
--
INSERT INTO `user_access` (`uid`, `cid`) VALUES
(1, 1),
(1, 2),
(1, 3),
(1, 4),
(1, 5),
(1, 6),
(1, 55);
--
-- Constraints for dumped tables
--
--
-- Constraints for table `user_access`
--
ALTER TABLE `user_access`
ADD CONSTRAINT `user_access_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `users` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `user_access_ibfk_2` FOREIGN KEY (`cid`) REFERENCES `creatures` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE;
This is the sql i've gotten down to
SELECT c.ID
FROM creatures AS c
RIGHT JOIN user_access AS a ON a.cid = c.ID
LEFT JOIN creatures_chances AS cc ON cc.ID = c.rarity
WHERE c.type = 'Egg'
AND a.uid =u.ID
AND round(RAND( ) *100)
BETWEEN cc.begin
AND cc.end
LIMIT 1
It all works, except for the
AND round(RAND( ) *100)
BETWEEN cc.begin
AND cc.end
part since if i run it, no matter what r is, the creature is always common, and sometimes it comes back with 0 rows, even though theres 1 of each type available
It also won't allow me to create a foreign key for the rarity, it just gives this error
SQL query:
ALTER TABLE creatures ADD FOREIGN KEY ( rarity ) REFERENCES creatures_chances ( ID ) ON UPDATE CASCADE ON DELETE CASCADE
MySQL said: Documentation
#1005 - Can't create table './calona/#sql-16d0_f3e83.frm' (errno: 150)
Any ideas for either problems