简体   繁体   English

Codeigniter MySQL列where子句中的未知列

[英]codeigniter mysql column unknown column in where clause

I have declared the variable upload_source in SELECT but get error 我在SELECT中声明了变量upload_source ,但出现错误

Unknown column 'upload_source' in 'where clause' “ where子句”中的未知列“ upload_source”

   SELECT b.name as name, filename, upload_date, IF(filename LIKE '%.png%', 1, 0) as type, IF(ex_link!='', 0, 1) as upload_source, title, a.id
    FROM (`all_media` a)
    LEFT JOIN `admins` b ON `b`.`id`=`user_id`
    WHERE `a`.`approved` =  0
    AND `upload_source` =  0
    ORDER BY `filename` DESC

How do i solve? 我该如何解决?

You cannot use aliased expression/column in where clause. 不能在where子句中使用别名表达式/列。 Use the same expression (which you defined in the Select clause), in the Where clause. Where子句中使用相同的表达式(在Select子句中定义)。

From MySQL Documentation : MySQL文档

An alias can be used in a query select list to give a column a different name. 可以在查询选择列表中使用别名来为列指定其他名称。 You can use the alias in GROUP BY, ORDER BY, or HAVING clauses to refer to the column. 您可以在GROUP BY,ORDER BY或HAVING子句中使用别名来引用该列。

Standard SQL disallows references to column aliases in a WHERE clause. 标准SQL不允许在WHERE子句中引用列别名。 This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined. 之所以施加此限制,是因为在评估WHERE子句时,可能尚未确定列值。

Do the following instead (I have improvised the upload_source = 0 to ex_link != '' ): 改为执行以下操作(我已将upload_source = 0 ex_link != ''ex_link != '' ):

 SELECT b.name as name, 
        filename, 
        upload_date, 
        IF(filename LIKE '%.png%', 1, 0) as type, 
        IF(ex_link!='', 0, 1) as upload_source, 
        title, 
        a.id
 FROM `all_media` AS a 
 LEFT JOIN `admins` b ON `b`.`id`=`user_id`
 WHERE `a`.`approved` =  0
   AND ex_link != '' 
 ORDER BY `filename` DESC

In SQL the WHERE keyword could not be used with aggregate functions . 在SQL中, WHERE关键字不能与聚合函数一起使用。 Try this with Having Clause : 尝试使用Haveing Clause

SELECT b.name as name, filename, upload_date, IF(filename LIKE '%.png%', 1, 0) as type, IF(ex_link!='', 0, 1) as upload_source,title, a.id FROM ( all_media a) LEFT JOIN admins b ON b . SELECT b.name as name, filename, upload_date, IF(filename LIKE '%.png%', 1, 0) as type, IF(ex_link!='', 0, 1) as upload_source,title, a.id FROM ( all_media a) LEFT JOIN管理员b ON b . id = user_id WHERE a . id = user_id,在WHERE . approved = 0 HAVING upload_source = 0 ORDER BY filename DESC 批准= 0 HAVING upload_source = 0 ORDER BY文件名DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM