試試這個:
SELECT count(taba.id)
FROM (
SELECT alum.id
FROM cvm_education AS edu
JOIN cvm_alumni AS alum ON alum.id = edu.alumni_id
WHERE alum.profile_status =1
AND highest_edu
IN (
SELECT name
FROM cvm_filter_educationlevels
JOIN cvm_educationlevel AS edulevels ON educationlevel_id = edulevels.id
WHERE filter_id = 1
)
) AS taba ;
http://www.sqlfiddle.com/#!2/f8adc/15
ŧ WO要點:
- 我不明白,使用SQL_CALC_FOUND_ROWS的()如果計數已偶然 它()。我認爲這提供了相同的期望結果。
- 您尚未提供樣本數據,因此我無法嘗試%s。我有 用二進制(1,0)代替它。此外,我不知道你的 確切的代碼,所以我根據你的查詢做了一些假設。
的樣本數據:
CREATE TABLE cvm_education(
ID int auto_increment primary key,
alumni_id int
);
CREATE TABLE cvm_alumni(
ID int auto_increment primary key,
profile_status int,
highest_edu varchar(30)
);
CREATE TABLE cvm_filter_educationlevels (
ID int auto_increment primary key,
educationlevel_id int,
name varchar(30)
);
CREATE TABLE cvm_educationlevel(
ID int auto_increment primary key,
filter_id int
);
INSERT INTO cvm_education (alumni_id)
VALUES (10), (1), (2), (3),(5), (6),(7),(8),(9);
INSERT INTO cvm_alumni (profile_status, highest_edu)
VALUES (1, "master"),
(0,"bachelor"),
(1,"bachelor"),
(0, "master"),
(1, "master"),
(0, "master"),
(1, "master"),
(1, "master"),
(1, "master"),
(1, "master");
INSERT INTO cvm_filter_educationlevels(educationlevel_id,name)
VALUES (1, "master"), (0,"bachelor");
INSERT INTO cvm_educationlevel(filter_ID)
VALUES (1), (0), (1), (0), (0), (1),(1),(1),(1);
你缺少一個右括號 - 你有三公,只有兩個關閉 – bhttoan
注意到了,但不知道從哪裏最後支架 – nv39
添加@ nv39這樣的事') AS taba)xxx' –