选择具有2个条件的计数

时间:2016-06-23 12:22:41

标签: mysql random time

我有2个表, med viz ,结构如下:

MED

CREATE TABLE med 
(id_med INT AUTO_INCREMENT PRIMARY KEY,
name_m VARCHAR(50),
surname_m VARCHAR(50),
spec VARCHAR(50),
UNIQUE (surname_m,name_m));

CREATE TABLE viz
(id_m INT NOT NULL,
id_p INT  NOT NULL,
id_c INT  NOT NULL,
dt DATETIME)

我需要一个选择查询,该查询将提供med的全名,其中id_p< 250的计数和dt的年份是2010。 以下查询给出了一个错误的结果:

select count(id_p)<250 as no_p,
(select distinct concat(name_m,' ',surname_m) from med where id_med=id_m) as fullname
from viz where year(dt)=2010;

1 个答案:

答案 0 :(得分:0)

尝试以下查询:

SELECT concat(m.name_m,' ',m.surname_m) AS fullname, COUNT(v.id_p)
FROM med m JOIN viz v
ON m.id_med = v.id_m
WHERE year(dt)=2010
GROUP BY m.id_med
HAVING COUNT(v.id_p) < 250