查询 mysql 分组依据和具有

query mysql grupo by and having

我正在尝试创建一个查询以获取所有日期的商业广告。

我需要这个 return:

商业广告 1 - 3 商业 2 - 4 商业 5 - 2 ...

但我需要从当前日期开始获取日期。我正在处理这个:

select count(id) 
from cita 
group by id_comercial
having date('created_at') = CURDATE()

这个 return 错误如果我删除 having 条款,return 我的 table 我需要的所有数据,但我需要按当前日期过滤。

在我需要在 eloquent 中转换此查询之后...但首先我必须在 mysql 中执行并显示结果。

我的table是:

    CREATE TABLE `cita` (
  `id` int(10) UNSIGNED NOT NULL,
  `nomape` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `direccion` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `provincia` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
  `ciudad` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
  `cp` char(5) COLLATE utf8mb4_unicode_ci NOT NULL,
  `telefono` varchar(12) COLLATE utf8mb4_unicode_ci NOT NULL,
  `movil` varchar(12) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `id_comercial` int(10) UNSIGNED DEFAULT NULL,
  `id_comercial2` int(10) UNSIGNED DEFAULT NULL,
  `id_llamada` int(10) UNSIGNED NOT NULL,
  `id_estado` int(10) UNSIGNED NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `cita` (`id`, `nomape`, `direccion`, `provincia`, `ciudad`, `cp`, `telefono`, `movil`, `id_comercial`, `id_comercial2`, `id_llamada`, `id_estado`, `created_at`, `updated_at`) VALUES
(1, 'ASENJO MARTIN JOSE RAFAEL', 'AV DE LA VEGA, 188', 'GRANADA', 'BELICENA', '18101', '958431322', NULL, 13, NULL, 617, 8, '2020-08-11 07:53:30', '2021-08-25 13:01:21'),
(2, 'MARTINEZ JIMENEZ MARIA LUISA', 'AVDA. ANDALUCIA, 3 BAJO F', 'JAÉN', 'TORREPEROGIL', '23320', '953776094', NULL, 44, NULL, 318, 8, '2020-08-11 09:20:55', '2021-08-24 15:41:03'),
(3, 'MARIA VIUDA DE SALVADOR LARA HURTADO', 'CALLE SANTA MARIA, 69', 'JAÉN', 'TORREPEROGIL', '23320', '953776876', NULL, 40, NULL, 578, 8, '2020-08-11 09:25:33', '2021-08-24 15:41:03'),
(4, 'MARTINEZ RUIZ EMILIO', 'CALLE OBISPO BASULTO, 97', 'JAÉN', 'TORREPEROGIL', '23320', '953776072', NULL, 42, NULL, 403, 8, '2020-08-11 09:29:02', '2021-08-24 15:41:03'),
(5, 'PATON REDONDO FERNANDO', 'CALLE DOCTOR FLEMING, 24 SIN LETRA', 'JAÉN', 'TORREPEROGIL', '23320', '953778119', NULL, 43, NULL, 438, 8, '2020-08-11 09:32:16', '2021-08-24 15:41:03'),
(6, 'VILLAR CRESPO JOSE', 'CALLE CERVANTES, 6 CASA', 'JAÉN', 'TORREPEROGIL', '23320', '953776353', NULL, 37, NULL, 769, 8, '2020-08-11 09:37:24', '2021-08-24 15:41:03'),
(7, 'MEDINA GALLEGO CONCEPCION', 'TRVA OBISPO BASULTO, 16', 'JAÉN', 'TORREPEROGIL', '23320', '953776523', NULL, 42, NULL, 404, 8, '2020-08-11 09:46:53', '2021-08-24 15:41:03'),
(8, 'BLANCO ANGUIS MARIA', 'CALLE MURILLO, 18 CASA', 'JAÉN', 'TORREPEROGIL', '23320', '953777328', NULL, 44, NULL, 526, 8, '2020-08-11 09:59:39', '2021-08-24 15:41:03'),
(9, 'MEZGUA MARTINEZ MATILDE', 'B. P. IGLESIAS, 4   2 D', 'JAÉN', 'TORREPEROGIL', '23320', '953778009', NULL, 37, NULL, 852, 8, '2020-08-11 10:00:52', '2021-08-24 15:41:04'),
(10, 'MARQUEZ MUÑOZ ANTONIO BLAS,ROSARIO', 'CALLE REAL DE BELICENA, 9 CASA', 'GRANADA', 'BELICENA', '18101', '958432077', NULL, 35, NULL, 952, 8, '2020-08-11 10:11:16', '2021-08-24 15:41:04'),
(11, 'BLAZQUEZ DELGADO ANTONIO Y EVA', 'CALLE JUAN MANUEL SERRAT, 9 CASA', 'GRANADA', 'BELICENA', '18101', '958431493', NULL, 32, NULL, 692, 2, '2020-08-11 10:13:31', '2020-08-11 13:10:49'),
(12, 'ALANIS GARCIA ANTONIO', 'CALLE RAMÓN Y CAJAL NUMERO 1', 'GRANADA', 'BELICENA', '18101', '958432049', NULL, 36, NULL, 723, 4, '2020-08-11 10:16:47', '2020-08-11 10:33:23'),

谢谢你的帮助,对不起我的英语,如果我的问题不好

having 子句在 聚合后执行。而且您只有一列 - 计数,而不是创建。

你想要一个 where 子句:

select count(id) 
from cita 
where date(created_at) = CURDATE()
group by id_comercial;

另请注意,我删除了单引号。我假设这些是你写问题时的转录错误。

此外,where 子句将更有效地写为:

where created_at >= CURDATE() and
      created_at < CURDATE() + interval 1 day

这有助于优化器,因为列上没有函数调用。