MySQL查询不考虑GROUP BY

时间:2014-02-10 10:54:31

标签: php mysql sql

我遇到了另一个SQL查询的问题。 该查询应该返回1的'nb_annonces',因为只有一行具有优于今天的'annonce_dispo_date'。 当我尝试获取行时,相同类型的查询有效,但在此尝试获取行数时,它会将'annonce_dispo'的数量返回为'nb_annonces'

SELECT `annonce`.`id` , CEIL( MAX( price ) * 1.16 ) AS `max_price` , COUNT( DISTINCT annonce.id ) AS `nb_annonces` , COUNT( annonce_dispo.annonce_dispo_id ) AS `nb_dispo`
FROM `annonce`
LEFT JOIN `annonce_dispo` ON ( annonce_dispo.annonceId = annonce.id
AND STR_TO_DATE( annonce_dispo.dispo_date, '%d/%m/%Y' ) >= CURDATE( ) )
WHERE (
(
annonce.price * 1.16
) >=0
)
AND (
annonce.visible =1
)
AND (
annonce.completed =1
)
GROUP BY `annonce`.`id`
HAVING (
nb_dispo >=1
)

返回

id  max_price   nb_annonces     nb_dispo
337     12           19         19

想要的结果:

id  max_price   nb_annonces     nb_dispo
#   12           2            #

#代表一个我并不在乎的数字。

对应的唯一行有19'annonce_dispos'优于今天,因此19

似乎没有将GROUP BY考虑在内......

我可以做些什么来获得适当的结果?

编辑(添加DISCTINCT后)

现在得到:

id  max_price   nb_annonces     nb_dispo
106     16            1          233
337     12            1          19

这是非常连贯的,但我只想要一行与nb_annonce对应的'annonce'对应的数字

编辑#TABLE STRUCTURE

CREATE TABLE IF NOT EXISTS `annonce_dispo` (
  `annonce_dispo_id` int(11) NOT NULL AUTO_INCREMENT,
  `userId` int(11) NOT NULL,
  `annonceId` int(11) NOT NULL,
  `created` datetime NOT NULL,
  `dispo_day` int(11) NOT NULL,
  `dispo_month` int(11) NOT NULL,
  `dispo_year` int(11) NOT NULL,
  `dispo_date` varchar(11) CHARACTER SET utf8 NOT NULL,
  `dispo_price` float NOT NULL,
  `disponibility` varchar(11) NOT NULL DEFAULT 'unavailable',
  PRIMARY KEY (`annonce_dispo_id`),
  KEY `userId` (`userId`),
  KEY `annonceId` (`annonceId`),
  KEY `dispo_date` (`dispo_date`),
  KEY `disponibility` (`disponibility`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `annonce` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userId` int(11) NOT NULL,
  `title` text CHARACTER SET utf8,
  `content` text CHARACTER SET utf8,
  `roomtype` enum('appartment','house','office','loft','garage','chalet','room','couch') CHARACTER SET utf8 NOT NULL,
  `capacity` tinyint(4) NOT NULL,
  `surface` smallint(6) DEFAULT NULL,
  `streetnumber` smallint(6) DEFAULT NULL,
  `streetname` varchar(128) CHARACTER SET utf8 DEFAULT NULL,
  `adressComplement` text CHARACTER SET utf8,
  `city` varchar(128) CHARACTER SET utf8 DEFAULT NULL,
  `zipcode` int(6) DEFAULT NULL,
  `country` varchar(128) CHARACTER SET utf8 DEFAULT NULL,
  `departement` varchar(5) CHARACTER SET utf8 NOT NULL,
  `region` varchar(128) CHARACTER SET utf8 DEFAULT NULL,
  `lifeRulesDescription` text CHARACTER SET utf8,
  `doubleBedNb` tinyint(4) DEFAULT '0',
  `simpleBedNb` tinyint(4) DEFAULT '0',
  `couchNb` tinyint(4) DEFAULT '0',
  `roomNb` tinyint(4) DEFAULT '0',
  `bedroomNb` int(11) DEFAULT NULL,
  `optionsId` int(11) DEFAULT NULL,
  `smokers` tinyint(1) DEFAULT '1',
  `pets` tinyint(1) DEFAULT '1',
  `ecofriendly` tinyint(1) DEFAULT '0',
  `caution` smallint(6) DEFAULT '0',
  `cleaningPrice` smallint(6) DEFAULT '0',
  `mainPicLink` varchar(128) CHARACTER SET utf8 DEFAULT '/',
  `price` double DEFAULT NULL,
  `annoncePriceDev` text NOT NULL,
  `priceAfterWeek` double DEFAULT NULL,
  `priceAfterTwoWeeks` double DEFAULT NULL,
  `priceAfterMonth` double DEFAULT NULL,
  `priceWeek` double DEFAULT NULL,
  `priceTwoWeeks` double DEFAULT NULL,
  `priceMonth` double DEFAULT NULL,
  `degressive_activated` tinyint(4) DEFAULT '0',
  `completed` tinyint(1) DEFAULT '0',
  `views` int(11) DEFAULT '1',
  `rented` int(11) DEFAULT '0',
  `visible` tinyint(1) DEFAULT '0',
  `activation_mail` tinyint(4) DEFAULT '0',
  `modified` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `activated` timestamp NULL DEFAULT NULL,
  `created` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `userId` (`userId`),
  KEY `price` (`price`),
  KEY `city` (`city`),
  KEY `zipcode` (`zipcode`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

INSERT INTO `annonce` (`id`, `userId`, `title`, `content`, `roomtype`, `capacity`, `surface`, `streetnumber`, `streetname`, `adressComplement`, `city`, `zipcode`, `country`, `departement`, `region`, `lifeRulesDescription`, `doubleBedNb`, `simpleBedNb`, `couchNb`, `roomNb`, `bedroomNb`, `optionsId`, `smokers`, `pets`, `ecofriendly`, `caution`, `cleaningPrice`, `mainPicLink`, `price`, `annoncePriceDev`, `priceAfterWeek`, `priceAfterTwoWeeks`, `priceAfterMonth`, `priceWeek`, `priceTwoWeeks`, `priceMonth`, `degressive_activated`, `completed`, `views`, `rented`, `visible`, `activation_mail`, `modified`, `activated`, `created`) VALUES
(106, 17, 'test distance recherche', 'test distance recherche test distance recherche test distance recherche', 'appartment', 1, 33, 0, 'voie de vaise', '', '28825', 69120, '76', '69', '82', '', 1, 0, 0, 1, NULL, NULL, 1, 1, NULL, 0, 0, NULL, 13, 'euro', 33, 33, 33, 231, 462, 924, 0, 1, 18, NULL, 1, 0, '2014-02-10 12:06:37', '2014-02-10 12:06:37', '2013-06-19 11:12:51'),
(337, 17, 'sdasdasdasd', 'sdasdasdasdsdasdasdasdsdasdasdasdsdasdasdasdsdasdasdasd', 'appartment', 1, 22, 0, 'qweqw', '', '22859', 58110, '76', '58', '26', '', 1, 0, 0, 1, 0, NULL, 1, 0, NULL, 0, 0, NULL, 10, 'euro', 22, 22, 22, 154, 308, 616, 0, 1, 19, NULL, 1, 1, '2014-02-10 09:36:26', NULL, '2013-06-28 17:16:30');

INSERT INTO `annonce_dispo` (`annonce_dispo_id`, `userId`, `annonceId`, `created`, `dispo_day`, `dispo_month`, `dispo_year`, `dispo_date`, `dispo_price`, `disponibility`) VALUES
(1207, 17, 106, '2013-06-19 11:33:01', 1, 2, 2014, '01/02/2014', 10, 'rented'),
(1208, 17, 106, '2013-06-19 11:33:01', 2, 2, 2014, '02/02/2014', 10, 'rented'),
(1209, 17, 106, '2013-06-19 11:33:01', 3, 2, 2014, '03/02/2014', 10, 'rented'),
(1210, 17, 106, '2013-06-19 11:33:01', 4, 2, 2014, '04/02/2014', 10, 'rented'),
(1211, 17, 106, '2013-06-19 11:33:01', 5, 2, 2014, '05/02/2014', 10, 'rented'),
(1212, 17, 106, '2013-06-19 11:33:01', 6, 2, 2014, '06/02/2014', 10, 'rented'),
(1213, 17, 106, '2013-06-19 11:33:01', 7, 2, 2014, '07/02/2014', 10, 'rented'),
(1214, 17, 106, '2013-06-19 11:33:01', 8, 2, 2014, '08/02/2014', 10, 'rented'),
(1215, 17, 106, '2013-06-19 11:33:01', 9, 2, 2014, '09/02/2014', 10, 'rented'),
(1216, 17, 106, '2013-06-19 11:33:01', 10, 2, 2014, '10/02/2014', 10, 'rented'),
(1217, 17, 106, '2013-06-19 11:33:01', 11, 2, 2014, '11/02/2014', 10, 'rented'),
(1218, 17, 106, '2013-06-19 11:33:01', 12, 2, 2014, '12/02/2014', 10, 'rented'),
(1219, 17, 106, '2013-06-19 11:33:01', 13, 2, 2014, '13/02/2014', 10, 'rented'),
(1220, 17, 106, '2013-06-19 11:33:01', 14, 2, 2014, '14/02/2014', 10, 'rented'),
(1221, 17, 106, '2013-06-19 11:33:01', 15, 2, 2014, '15/02/2014', 10, 'rented'),
(1222, 17, 106, '2013-06-19 11:33:01', 16, 2, 2014, '16/02/2014', 10, 'rented'),
(1223, 17, 106, '2013-06-19 11:33:01', 17, 2, 2014, '17/02/2014', 10, 'rented'),
(1224, 17, 106, '2013-06-19 11:33:01', 18, 2, 2014, '18/02/2014', 10, 'rented'),
(1225, 17, 106, '2013-06-19 11:33:01', 19, 2, 2014, '19/02/2014', 10, 'rented'),
(1226, 17, 106, '2013-06-19 11:33:01', 20, 2, 2014, '20/02/2014', 10, 'rented'),
(1227, 17, 106, '2013-06-19 11:33:01', 21, 2, 2014, '21/02/2014', 10, 'rented'),
(1228, 17, 106, '2013-06-19 11:33:01', 22, 2, 2014, '22/02/2014', 10, 'rented'),
(1229, 17, 106, '2013-06-19 11:33:01', 23, 2, 2014, '23/02/2014', 10, 'rented'),
(1230, 17, 106, '2013-06-19 11:33:01', 24, 2, 2014, '24/02/2014', 10, 'rented'),
(1231, 17, 106, '2013-06-19 11:33:02', 25, 2, 2014, '25/02/2014', 10, 'rented'),
(1232, 17, 106, '2013-06-19 11:33:02', 26, 2, 2014, '26/02/2014', 10, 'rented'),
(1233, 17, 106, '2013-06-19 11:33:02', 27, 2, 2014, '27/02/2014', 10, 'rented'),
(1234, 17, 106, '2013-06-19 11:33:02', 28, 2, 2014, '28/02/2014', 10, 'rented'),
(1666, 17, 337, '2013-07-04 15:20:40', 1, 2, 2014, '01/02/2014', 10, 'available'),
(1667, 17, 337, '2013-07-04 15:20:40', 2, 2, 2014, '02/02/2014', 10, 'available'),
(1668, 17, 337, '2013-07-04 15:20:40', 3, 2, 2014, '03/02/2014', 10, 'available'),
(1669, 17, 337, '2013-07-04 15:20:40', 4, 2, 2014, '04/02/2014', 10, 'available'),
(1670, 17, 337, '2013-07-04 15:20:40', 5, 2, 2014, '05/02/2014', 10, 'available'),
(1671, 17, 337, '2013-07-04 15:20:41', 6, 2, 2014, '06/02/2014', 10, 'available'),
(1672, 17, 337, '2013-07-04 15:20:41', 7, 2, 2014, '07/02/2014', 10, 'available'),
(1673, 17, 337, '2013-07-04 15:20:41', 8, 2, 2014, '08/02/2014', 10, 'available'),
(1674, 17, 337, '2013-07-04 15:20:41', 9, 2, 2014, '09/02/2014', 10, 'available'),
(1675, 17, 337, '2013-07-04 15:20:41', 10, 2, 2014, '10/02/2014', 10, 'available'),
(1676, 17, 337, '2013-07-04 15:20:41', 11, 2, 2014, '11/02/2014', 10, 'available'),
(1677, 17, 337, '2013-07-04 15:20:41', 12, 2, 2014, '12/02/2014', 10, 'available'),
(1678, 17, 337, '2013-07-04 15:20:41', 13, 2, 2014, '13/02/2014', 10, 'available'),
(1679, 17, 337, '2013-07-04 15:20:41', 14, 2, 2014, '14/02/2014', 10, 'available'),
(1680, 17, 337, '2013-07-04 15:20:41', 15, 2, 2014, '15/02/2014', 10, 'available'),
(1681, 17, 337, '2013-07-04 15:20:41', 16, 2, 2014, '16/02/2014', 10, 'available'),
(1682, 17, 337, '2013-07-04 15:20:41', 17, 2, 2014, '17/02/2014', 10, 'available'),
(1683, 17, 337, '2013-07-04 15:20:41', 18, 2, 2014, '18/02/2014', 10, 'available'),
(1684, 17, 337, '2013-07-04 15:20:41', 19, 2, 2014, '19/02/2014', 10, 'available'),
(1685, 17, 337, '2013-07-04 15:20:41', 20, 2, 2014, '20/02/2014', 10, 'available'),
(1686, 17, 337, '2013-07-04 15:20:41', 21, 2, 2014, '21/02/2014', 10, 'available'),
(1687, 17, 337, '2013-07-04 15:20:41', 22, 2, 2014, '22/02/2014', 10, 'available'),
(1688, 17, 337, '2013-07-04 15:20:41', 23, 2, 2014, '23/02/2014', 10, 'available'),
(1689, 17, 337, '2013-07-04 15:20:41', 24, 2, 2014, '24/02/2014', 10, 'available'),
(1690, 17, 337, '2013-07-04 15:20:41', 25, 2, 2014, '25/02/2014', 10, 'available'),
(1691, 17, 337, '2013-07-04 15:20:41', 26, 2, 2014, '26/02/2014', 10, 'available'),
(1692, 17, 337, '2013-07-04 15:20:41', 27, 2, 2014, '27/02/2014', 10, 'available'),
(1693, 17, 337, '2013-07-04 15:20:41', 28, 2, 2014, '28/02/2014', 10, 'available');

1 个答案:

答案 0 :(得分:1)

将DISTINCT添加到nb_annonces

以下是代码:

  

COUNT(DISTINCT annonce.id)AS nb_annonces