par
supercanard » 30 sept. 2008, 00:00
Bonsoir,
Voilà après avoir retourner dans tous les sens le peu de doc que j'ai pu trouver à propos d'UNION, essayé des syntaxes différentes je vient chercher vos éclaircissements.
Je cherche tout simplement à faire un count sur 2 tables dans une seule requête mais ça n'a pas l'air aussi simple que ça en fait
SELECT COUNT( id_post ) AS nbPost
FROM post
WHERE id_membre =1
UNION SELECT COUNT( id_topic ) AS nbTopic
FROM topic
WHERE id_membre =1
Le résultat me retourne 2 valeurs complètement farfelues pour nbPost ( pourquoi 2 déjà c'est une bonne question) et rien du tout pour nbTopic.
Dans mon code php par contre j'ai une erreur : Notice: Undefined index: nbTopic in... etc
Code : Tout sélectionner
CREATE TABLE `post` (
`id_post` int(11) NOT NULL auto_increment,
`id_topic` int(11) NOT NULL,
`id_membre` int(11) NOT NULL,
`date` datetime NOT NULL,
`message` text NOT NULL,
PRIMARY KEY (`id_post`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=51 ;
CREATE TABLE `topic` (
`id_topic` int(11) NOT NULL auto_increment,
`id_membre` int(11) NOT NULL,
`nom` varchar(250) NOT NULL,
`date` datetime NOT NULL,
`postit` tinyint(1) NOT NULL,
`test` tinyint(1) NOT NULL,
PRIMARY KEY (`id_topic`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=27 ;
Bonsoir,
Voilà après avoir retourner dans tous les sens le peu de doc que j'ai pu trouver à propos d'UNION, essayé des syntaxes différentes je vient chercher vos éclaircissements.
Je cherche tout simplement à faire un count sur 2 tables dans une seule requête mais ça n'a pas l'air aussi simple que ça en fait
[php] SELECT COUNT( id_post ) AS nbPost
FROM post
WHERE id_membre =1
UNION SELECT COUNT( id_topic ) AS nbTopic
FROM topic
WHERE id_membre =1 [/php]
Le résultat me retourne 2 valeurs complètement farfelues pour nbPost ( pourquoi 2 déjà c'est une bonne question) et rien du tout pour nbTopic.
Dans mon code php par contre j'ai une erreur : Notice: Undefined index: nbTopic in... etc
:?
[code]CREATE TABLE `post` (
`id_post` int(11) NOT NULL auto_increment,
`id_topic` int(11) NOT NULL,
`id_membre` int(11) NOT NULL,
`date` datetime NOT NULL,
`message` text NOT NULL,
PRIMARY KEY (`id_post`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=51 ;
CREATE TABLE `topic` (
`id_topic` int(11) NOT NULL auto_increment,
`id_membre` int(11) NOT NULL,
`nom` varchar(250) NOT NULL,
`date` datetime NOT NULL,
`postit` tinyint(1) NOT NULL,
`test` tinyint(1) NOT NULL,
PRIMARY KEY (`id_topic`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=27 ;[/code]