Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Fala galera, eu estou fazendo uma estatística para ver quantos usuários compraram...
aqui minhas tabelas.:
CREATE TABLE `buy` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`products_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`date_buy` datetime DEFAULT NULL,
`date_block` datetime DEFAULT NULL,
`code` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
`liberated` int(1) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `sold` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`products_id` int(11) DEFAULT NULL,
`sold` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`informations_id` int(11) NOT NULL,
`city_id` int(11) NOT NULL,
`partner_id` int(11) NOT NULL,
`file_upload_id` int(11) NOT NULL,
`name` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`permalink` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`twitter` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`of` decimal(5,2) DEFAULT NULL,
`per` decimal(5,2) DEFAULT NULL,
`porcent` int(3) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`end` datetime DEFAULT NULL,
`block` datetime DEFAULT NULL,
`limits` int(11) DEFAULT NULL,
`stock` int(11) DEFAULT NULL,
`norder` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
e aqui minha Query:
SELECT COUNT( user.id ) AS total FROM buy INNER JOIN USER ON ( user.id = buy.user_id ) INNER JOIN products ON ( products.id = buy.products_id ) INNER JOIN sold ON ( sold.products_id = products.id ) GROUP BY user.id, buy.user_id;
Só que assim, na minha tabela buy, eu tenho 10 registros do id 1 e uma do registro 3, então essa query retorna 10-1, e eu queria que ele me retornasse apenas 2 :lol:
como faço isso ?
Obrigado.
Carregando comentários...