此问题已在此处有答案:
Unknown Column In Where Clause(16个答案)
3天前关闭。
我合并2表中的数据在查询中显示分页,但在第二个查询中的过滤器,我没有在第二个查询中的客户端名称,但当我过滤的数据第二个表中的数据出现,所以我想添加不为空,但得到未知的列错误。
(
select
`linguists`.`id` as `linguist_id`,
CONCAT(linguists.first_name," ", linguists.last_name) AS linguist_name,
`reps`.`id` as `rep_id`,
CONCAT(reps.first_name, " ", reps.last_name) AS rep_name,
`clients`.`id` as `client_id`,
`clients`.`name` as `client_name`,
`clients`.`type` as `client_type`,
`invoices`.`booking_id`,
NULL AS id,
`bookings`.`booking_date`,
`invoices`.`client_paid`,
`invoices`.`linguist_paid`,
`invoices`.`demand_letter_id`,
`invoices`.`reminder_1`,
`invoices`.`reminder_2`,
`invoices`.`reminder_3`,
`invoices`.`reminder_4`,
`invoices`.`exceptional_case`,
bookings.source_company AS scope,
`bookings`.`credit_note`,
DATEDIFF(CURDATE(), invoices.created_at) AS days_since_invoice,
NULL AS start_date,
NULL AS end_date
from `invoices`
left join `bookings` on `bookings`.`id` = `invoices`.`booking_id`
left join `clients` on `clients`.`id` = `bookings`.`client_id`
left join `reps` on `reps`.`id` = `bookings`.`rep_id`
left join `linguists` on `linguists`.`id` = `bookings`.`linguist_id`
where
(
`clients`.`name` like '%t%'
or
`clients`.`id` = 't'
)
and `invoices`.`linguist_paid` is null
and `invoices`.`invoice_submitted_linguist` < '2023-04-12'
and `bookings`.`source_company` = 'UKLS'
)
union all
(
select
`linguist_id`,
CONCAT(linguists.first_name, " ", linguists.last_name) AS linguist_name,
NULL AS rep_id,
NULL AS rep_name,
NULL AS client_id,
NULL AS client_name,
NULL AS client_type,
`on_demand_linguist_invoice`.`id`,
NULL AS booking_id,
NULL AS booking_date,
NULL AS client_paid,
`linguist_paid`,
NULL AS demand_letter_id,
NULL AS reminder_1,
NULL AS reminder_2,
NULL AS reminder_3,
NULL AS reminder_4,
NULL AS exceptional_case,
NULL AS scope,
NULL AS credit_note,
DATEDIFF(CURDATE(), on_demand_linguist_invoice.created_at) AS days_since_invoice,
`on_demand_linguist_invoice`.`start_date`,
`on_demand_linguist_invoice`.`end_date`
from `on_demand_linguist_invoice`
left join `linguists` on `linguists`.`id` = `on_demand_linguist_invoice`.`linguist_id`
where
(
client_name is not null
)
and `on_demand_linguist_invoice`.`linguist_paid` is null
and `on_demand_linguist_invoice`.`invoice_submitted_linguist` < '2023-04-12'
)
我尝试使用where client_name不为null,因此第二个表数据不会出现,因为我在select中选择了NULL作为client_name,但在“where子句”中获得了未知列“client_name”
1条答案
按热度按时间u0njafvf1#
我在select中选择NULL作为client_name,但在“where子句”中获得未知列“client_name”
client_name
是输出列的别名。但是您不能在WHERE中使用输出列名,因为它在查询执行的这一步中不存在。您可以使用输出列表达式代替别名(在本例中为
WHERE NULL IS NULL
),也可以将此条件移动到HAVING子句(在本例中为WHERE ... HAVING client_name IS NULL
)