好的,所以我有一个简单的Mysql查询:
SELECT *
FROM student
LEFT JOIN student_state ON student.student_id = student_state.student_id_fk
WHERE student_surname LIKE '%XXX%'
OR student_lastname LIKE '%XXX%'
它返回一些结果。
我想获得这些结果,并通过第二个选择像下面这样进一步过滤:
SELECT *
FROM
(SELECT student_payment
FROM student
LEFT JOIN student_state ON student.student_id = student_state.student_id_fk
WHERE student_surname LIKE '%XXX%'
OR student_lastname LIKE '%XXX%') AS test
WHERE
student_payment LIKE '%XXX%'
OR student_unlock LIKE '%XXX%'
当我仅将第一个SELECT
插入到FROM
括号中时,我收到一个错误,提示需要别名。当我为子查询提供别名时,它说
未知列student_payment或student_unlock
有什么想法吗?
答案 0 :(得分:2)
您的第一个查询仅返回1列,但在第二个查询中,您尝试按2列进行过滤。
因此,同时包含两列是您的第一个查询:
SELECT t.* FROM (
SELECT student_payment, student_unlock
FROM student LEFT JOIN student_state
ON student.student_id = student_state.student_id_fk
WHERE student_surname LIKE '%XXX%' OR student_lastname LIKE '%XXX%'
) t
WHERE t.student_payment LIKE '%XXX%' OR t.student_unlock LIKE '%XXX%'
您可以通过以下方法获得相同的结果:
SELECT student_payment, student_unlock
FROM student LEFT JOIN student_state
ON student.student_id = student_state.student_id_fk
WHERE
(student_surname LIKE '%XXX%' OR student_lastname LIKE '%XXX%')
AND
(t.student_payment LIKE '%XXX%' OR t.student_unlock LIKE '%XXX%')