PHP expert |
|
Ik heb een probleem met een query.
Dit zijn mijn tabellen:
CREATE TABLE `posts` (
`ID` int(11) NOT NULL auto_increment,
`threadID` int(11) NOT NULL default '0',
`titel` text NOT NULL,
`bericht` text NOT NULL,
`poster_naam` text NOT NULL,
`poster_email` text NOT NULL,
PRIMARY KEY (`ID`)
) TYPE=MyISAM AUTO_INCREMENT=6 ;
INSERT INTO `posts` VALUES (1, 1, 'Post1', 'Post1', '', '');
INSERT INTO `posts` VALUES (2, 1, 'Post2', 'Post2', '', '');
INSERT INTO `posts` VALUES (4, 1, 'Post4', 'Post4', '', '');
CREATE TABLE `posts` ( `ID` int(11) NOT NULL auto_increment, `threadID` int(11) NOT NULL default '0', `titel` text NOT NULL, `bericht` text NOT NULL, `poster_naam` text NOT NULL, `poster_email` text NOT NULL, ) TYPE=MyISAM AUTO_INCREMENT=6 ; INSERT INTO `posts` VALUES (1, 1, 'Post1', 'Post1', '', ''); INSERT INTO `posts` VALUES (2, 1, 'Post2', 'Post2', '', ''); INSERT INTO `posts` VALUES (4, 1, 'Post4', 'Post4', '', '');
CREATE TABLE `threads` (
`ID` int(11) NOT NULL auto_increment,
`categorieID` int(11) NOT NULL default '0',
`titel` text NOT NULL,
`bericht` text NOT NULL,
`poster_naam` text NOT NULL,
`poster_email` text NOT NULL,
PRIMARY KEY (`ID`)
) TYPE=MyISAM AUTO_INCREMENT=6 ;
INSERT INTO `threads` VALUES (1, 1, 'thread 1', 'thread 1 bericht', 'Fenrir', 'fenrir.jules@gmail.com');
INSERT INTO `threads` VALUES (2, 1, 'thread 2', 'thread 2 bericht', 'Fenrir', 'fenrir.jules@gmail.com');
INSERT INTO `threads` VALUES (4, 2, 'thread 4', 'thread 4 bericht', 'Fenrir', 'fenrir.jules@gmail.com');
INSERT INTO `threads` VALUES (3, 2, 'thread 3', 'thread 3 bericht', 'Fenrir', 'fenrir.jules@gmail.com');
INSERT INTO `threads` VALUES (5, 1, 'thread5', 'thread5', '', '');
CREATE TABLE `threads` ( `ID` int(11) NOT NULL auto_increment, `categorieID` int(11) NOT NULL default '0', `titel` text NOT NULL, `bericht` text NOT NULL, `poster_naam` text NOT NULL, `poster_email` text NOT NULL, ) TYPE=MyISAM AUTO_INCREMENT=6 ; INSERT INTO `threads` VALUES (1, 1, 'thread 1', 'thread 1 bericht', 'Fenrir', 'fenrir.jules@gmail.com'); INSERT INTO `threads` VALUES (2, 1, 'thread 2', 'thread 2 bericht', 'Fenrir', 'fenrir.jules@gmail.com'); INSERT INTO `threads` VALUES (4, 2, 'thread 4', 'thread 4 bericht', 'Fenrir', 'fenrir.jules@gmail.com'); INSERT INTO `threads` VALUES (3, 2, 'thread 3', 'thread 3 bericht', 'Fenrir', 'fenrir.jules@gmail.com'); INSERT INTO `threads` VALUES (5, 1, 'thread5', 'thread5', '', '');
En dit is mijn query:
SELECT
threads.ID as ID,
threads.titel as titel,
COUNT(posts.ID) as posts
FROM threads
LEFT JOIN posts
ON posts.threadID=threads.ID
WHERE threads.categorieID='.$ID.'
GROUP BY posts.threadID
ORDER BY threads.titel
SELECT threads.ID as ID, threads.titel as titel, FROM threads ON posts.threadID=threads.ID WHERE threads.categorieID='.$ID.' GROUP BY posts.threadID ORDER BY threads.titel
nu krijg ik deze data:
array (
0 =>
array (
'ID' => '1',
'titel' => 'thread 1',
'posts' => '3',
),
1 =>
array (
'ID' => '2',
'titel' => 'thread 2',
'posts' => '0',
),
)
0 => 'ID' => '1', 'titel' => 'thread 1', 'posts' => '3', ), 1 => 'ID' => '2', 'titel' => 'thread 2', 'posts' => '0', ), )
maar ik wil deze data krijgen:
array (
0 =>
array (
'ID' => '1',
'titel' => 'thread 1',
'posts' => '3',
),
1 =>
array (
'ID' => '2',
'titel' => 'thread 2',
'posts' => '0',
),
2 =>
array (
'ID' => '5',
'titel' => 'thread5',
'posts' => '0',
),
)
0 => 'ID' => '1', 'titel' => 'thread 1', 'posts' => '3', ), 1 => 'ID' => '2', 'titel' => 'thread 2', 'posts' => '0', ), 2 => 'ID' => '5', 'titel' => 'thread5', 'posts' => '0', ), )
Dus er zit een fout in mij query dat als een thread 0 posts heeft, dat de volgende thread dan niet meer geselecteerd wordt.
Wat doe ik fout?
mvd,
Fenrir
|