2

我有以下查询:

SELECT
(...),
(SELECT us.short FROM url_short us WHERE us.urlId = u.id
  ORDER BY us.id DESC LIMIT 1) as short, 
(...),
(SELECT f.name FROM `group` f LEFT JOIN group_url fu ON f.id = fu.group_id WHERE
  fu.url_id = u.id ORDER BY f.id DESC LIMIT 1) as f_name
FROM (...)
WHERE (...) AND
(u.url LIKE '%ops%'
OR short LIKE '%ops%'
OR u.url_name LIKE '%ops%'
OR f_name LIKE '%ops%')
(...)

但是,当我尝试在shortf_groupMySQL 中使用 LIKE 时,我会说:

#1054 - Unknown column 'short' in 'where clause'

我已经搜索了很多,但一无所获

4

2 回答 2

2

您的 where 子句看不到short别名,因为它仅通过在过滤结果集(由最外层子句实现)之后发生的投影步骤引入。WHERE

尝试

SELECT *
  FROM (
          SELECT
          (...),
          (SELECT us.short FROM url_short us WHERE us.urlId = u.id
            ORDER BY us.id DESC LIMIT 1) as short, 
          (...),
          (SELECT f.name FROM `group` f LEFT JOIN group_url fu ON f.id = fu.group_id WHERE
            fu.url_id = u.id ORDER BY f.id DESC LIMIT 1) as f_name
          FROM (...)
          WHERE (...)
       ) emb
   WHERE (emb.url LIKE '%ops%'
          OR emb.short LIKE '%ops%'
          OR emb.url_name LIKE '%ops%'
          OR emb.f_name LIKE '%ops%')
       ;
于 2013-08-02T15:50:22.007 回答
1

您在查询中使用别名,这可能会导致此错误。 这是另一个有类似问题的问题。

于 2013-08-02T15:45:13.127 回答