conware
10-04-2011, 05:24 PM
Hi guys. Thanks to Wanna here on codingforums I was able to get my results from my mysql database with LEFT JOIN.
However I noticed that I had duplicated records in my output.
So basically i'll try to explane it the best I can.
I have three tables in my database.
CREATE TABLE IF NOT EXISTS `game_table` (
`g_id` int(11) NOT NULL AUTO_INCREMENT,
`g_title` varchar(255) NOT NULL,
PRIMARY KEY (`g_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
INSERT INTO `game_table` (`g_id`, `g_title`) VALUES
(1, '4-in-1 Fun Pack'),
(2, 'Kirby''s Dream Land'),
(3, 'Super Mario Land'),
(4, 'Super Mario Land 2: 6 Golden Coins'),
(5, 'Donkey Kong'),
(7, 'The Final Fantasy Legend'),
(8, 'Race Days: 2 Full Games on 1 Cartridge'),
(9, 'Final Fantasy Adventure');
CREATE TABLE IF NOT EXISTS `genre_table` (
`g_id` int(2) NOT NULL AUTO_INCREMENT,
`g_name` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`g_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=17 ;
INSERT INTO `genre_table` (`g_id`, `g_name`) VALUES
(1, 'Action'),
(2, 'Adventure'),
(3, 'Beat''em Up'),
(4, 'Board Game'),
(5, 'Editor'),
(6, 'Fighting'),
(7, 'Gambling'),
(8, 'Game Show'),
(9, 'Platformer'),
(10, 'Puzzle'),
(11, 'RPG'),
(12, 'Racing'),
(13, 'Simulation'),
(14, 'Sports'),
(15, 'Strategy'),
(16, 'Misc');
CREATE TABLE IF NOT EXISTS `track_genre` (
`game_id` int(11) NOT NULL,
`genre_id` int(3) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `track_genre` (`game_id`, `genre_id`) VALUES
(1, 15),
(5, 10),
(5, 9);
Now what I tried to do was use LEFT JOIN in my php script query.
To select the genre names and display them in a list next to the game titles.
However some games have more genres then one and some have null values.
And when I add a extra field in track table eq to the genre with the same id as the game I get Duplicated Records.
Is there a way to solve this?
My current query looks like this:
SELECT `game_table`.`g_title`, `genre_table`.`g_name`
FROM `game_table`
LEFT JOIN `track_genre`
ON `game_table`.`g_id`=`track_genre`.`game_id`
LEFT JOIN `genre_table`
ON `track_genre`.`genre_id`=`genre_table`.`g_id`
This returns Donkey Kong two times because it has two genres.
Is there a way to solve this?
However I noticed that I had duplicated records in my output.
So basically i'll try to explane it the best I can.
I have three tables in my database.
CREATE TABLE IF NOT EXISTS `game_table` (
`g_id` int(11) NOT NULL AUTO_INCREMENT,
`g_title` varchar(255) NOT NULL,
PRIMARY KEY (`g_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
INSERT INTO `game_table` (`g_id`, `g_title`) VALUES
(1, '4-in-1 Fun Pack'),
(2, 'Kirby''s Dream Land'),
(3, 'Super Mario Land'),
(4, 'Super Mario Land 2: 6 Golden Coins'),
(5, 'Donkey Kong'),
(7, 'The Final Fantasy Legend'),
(8, 'Race Days: 2 Full Games on 1 Cartridge'),
(9, 'Final Fantasy Adventure');
CREATE TABLE IF NOT EXISTS `genre_table` (
`g_id` int(2) NOT NULL AUTO_INCREMENT,
`g_name` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`g_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=17 ;
INSERT INTO `genre_table` (`g_id`, `g_name`) VALUES
(1, 'Action'),
(2, 'Adventure'),
(3, 'Beat''em Up'),
(4, 'Board Game'),
(5, 'Editor'),
(6, 'Fighting'),
(7, 'Gambling'),
(8, 'Game Show'),
(9, 'Platformer'),
(10, 'Puzzle'),
(11, 'RPG'),
(12, 'Racing'),
(13, 'Simulation'),
(14, 'Sports'),
(15, 'Strategy'),
(16, 'Misc');
CREATE TABLE IF NOT EXISTS `track_genre` (
`game_id` int(11) NOT NULL,
`genre_id` int(3) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `track_genre` (`game_id`, `genre_id`) VALUES
(1, 15),
(5, 10),
(5, 9);
Now what I tried to do was use LEFT JOIN in my php script query.
To select the genre names and display them in a list next to the game titles.
However some games have more genres then one and some have null values.
And when I add a extra field in track table eq to the genre with the same id as the game I get Duplicated Records.
Is there a way to solve this?
My current query looks like this:
SELECT `game_table`.`g_title`, `genre_table`.`g_name`
FROM `game_table`
LEFT JOIN `track_genre`
ON `game_table`.`g_id`=`track_genre`.`game_id`
LEFT JOIN `genre_table`
ON `track_genre`.`genre_id`=`genre_table`.`g_id`
This returns Donkey Kong two times because it has two genres.
Is there a way to solve this?