【发布时间】:2013-03-26 01:10:02
【问题描述】:
此查询在 where 子句中给出了一个错误 unknown column company。我发现 where 子句首先运行,然后 select 运行。这可能是这里的错误。但我不知道如何纠正这个问题以便让公司进入结果集。
SELECT trnsdriverid,
name,
(SELECT transcompany.name
FROM transcompany,
transcompdriver
WHERE transcompany.trnscompid = transcompdriver.trnscompid) AS 'company',
address,
dob,
license,
telephone
FROM transcompdriver
WHERE ? LIKE 'All'
OR name LIKE '%"+keyword+"%'
OR company LIKE '%"+keyword+"%'
OR trnsdriverid LIKE '%"+keyword+"%'
【问题讨论】:
-
那是因为您将嵌套选择的结果别名为“公司”——该列实际上并不存在。您不能在 where 语句中引用列别名。您应该重写此查询以使用 JOIN,然后对实际的
TransCompany.name列进行过滤。 -
公司列与其他列的行数不同
-
@Cory Thnx 为您的回复。我会尝试加入。
-
@user2033382:我回答了一个应该有效的查询。
-
@Cory 。是的,它工作正常。 Thnx 很多。不幸的是,由于缺乏声誉而无法投票。