为什么你需要把你自己创建的(例如列select 1 as "number"后)HAVING,而不是WHERE在MySQL?
是否有任何缺点而不是做WHERE 1(编写整个定义而不是列名)?
我有以下两个表:
1. Lecturers (LectID, Fname, Lname, degree).
2. Lecturers_Specialization (LectID, Expertise).
Run Code Online (Sandbox Code Playgroud)
我想找到专业化程度最高的讲师.当我尝试这个时,它不起作用:
SELECT
L.LectID,
Fname,
Lname
FROM Lecturers L,
Lecturers_Specialization S
WHERE L.LectID = S.LectID
AND COUNT(S.Expertise) >= ALL (SELECT
COUNT(Expertise)
FROM Lecturers_Specialization
GROUP BY LectID);
Run Code Online (Sandbox Code Playgroud)
但是当我尝试这个时,它有效:
SELECT
L.LectID,
Fname,
Lname
FROM Lecturers L,
Lecturers_Specialization S
WHERE L.LectID = S.LectID
GROUP BY L.LectID,
Fname,
Lname
HAVING COUNT(S.Expertise) >= ALL (SELECT
COUNT(Expertise)
FROM Lecturers_Specialization
GROUP BY LectID);
Run Code Online (Sandbox Code Playgroud)
是什么原因?谢谢.
我正在运行的查询如下,但是我收到此错误:
#1054 - 'IN/ALL/ANY子查询'中的未知列'guaranteed_postcode'
SELECT `users`.`first_name`, `users`.`last_name`, `users`.`email`,
SUBSTRING(`locations`.`raw`,-6,4) AS `guaranteed_postcode`
FROM `users` LEFT OUTER JOIN `locations`
ON `users`.`id` = `locations`.`user_id`
WHERE `guaranteed_postcode` NOT IN #this is where the fake col is being used
(
SELECT `postcode` FROM `postcodes` WHERE `region` IN
(
'australia'
)
)
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么我无法在同一个数据库查询的where子句中使用假列?