gpt4 book ai didi

mysql - 是否可以在 where 子句中使用命名选择?

转载 作者:行者123 更新时间:2023-11-29 14:11:56 24 4
gpt4 key购买 nike

我有一个半复杂的选择语句,它在查询中构建自定义“列”,并且需要按此列的结果过滤结果。有什么方法可以在谓词中引用该列吗?您将看到我想要在 where 子句中注释掉的内容,并筛选“特价”。

select
p.prod_id,
case
when p.subtitle is null then p.title
else concat(p.title, ': ', p.subtitle)
end as 'Title',
p.issue as 'Issue',
e.abbrv as 'Editor',
p.jobnum as 'Job Number',
p.price as 'Price',
ship.due_date as 'Ship Date',
case
when pi.onsale_minus_ship_date is not null then ship.due_date + interval pi.onsale_minus_ship_date day
else
case
when prs.days_for_shipping != 0 then ship.due_date + interval prs.days_for_shipping day
else ship.due_date + interval 7 day
end
end as 'On Sale',
sale.due_date as 'Bookstore On Sale'
from products p

join schedules ship on ship.prod_id = p.prod_id and ship.milestone = 49
left join schedules sale on sale.prod_id = p.prod_id and sale.milestone = 647
left join editors e on find_in_set(e.id, p.editor)
left join printing_info pi on pi.prod_id = p.prod_id
left join printers prs on prs.id = pi.printer

where p.prod_type in (2, 3, 5, 6) -- physical, non comics (trades, hc, etc.)
--and 'On Sale' >= '$start_date' + interval 2 month
--and 'On Sale' <= '$end_date' + interval 2 month

order by ship.due_date asc, p.title asc

最佳答案

您可以在 HAVING 子句中进行过滤 - 遗憾的是,您无法在 WHERE 子句中引用列别名。

HAVING `On Sale` >= '$start_date' + interval 2 month
AND `On Sale` <= '$end_date' + interval 2 month

http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html

关于mysql - 是否可以在 where 子句中使用命名选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13368003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com