MySQL 选择查询,从即时生成的列中选择位置

发布于 2024-10-03 11:30:26 字数 423 浏览 3 评论 0原文

我有一个(我的)SQL 问题。如果我有一张表,假设名为 Cars,具有各种列,其中两个是 INT,我想将其加在一起,一个称为后轮,另一个称为前轮。

时,我可以执行此查询将它们添加在一起

当我选择select (backwheels+frontwheels) as Totalwheels from Cars;

;效果很好,但是当我尝试在 Totalwheels 列上进行选择时,我得到错误

从汽车中选择(后轮+前轮)作为totalwheels,其中totalwheels = 4;

我得到的错误:

“where”中的未知列“totalwheels” 条款'

是否可以以某种方式从实际上不存在的列中进行选择?

I have a (my)SQL question. If I have a table, let's say called Cars with various columns, two of which are INT which I want to add together, one called backwheels and the other called frontwheels.

I can do this query to add them together when I select

select (backwheels+frontwheels) as totalwheels from Cars;

which works fine, but when I try and do a select where on that totalwheels column, I get an error

select (backwheels+frontwheels) as totalwheels from Cars where totalwheels=4;

the error I get back:

Unknown column 'totalwheels' in 'where
clause'

Is it possible to somehow select from a column which doesn't really exist like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自此以后,行同陌路 2024-10-10 11:30:26

您可以使用:

select (backwheels+frontwheels) as totalwheels from Cars
where (backwheels+frontwheels)=4;

或:

SELECT * from 
(select c.*, (backwheels+frontwheels) as totalwheels from Cars)
where totalwheels=4;

You can use:

select (backwheels+frontwheels) as totalwheels from Cars
where (backwheels+frontwheels)=4;

OR:

SELECT * from 
(select c.*, (backwheels+frontwheels) as totalwheels from Cars)
where totalwheels=4;
小草泠泠 2024-10-10 11:30:26

导致此错误的原因是您的表没有“totalwheels”列,并且您的 where 子句尝试在不存在的列上运行条件。

您可以尝试使用 -

select (backwheels+frontwheels) as totalwheels from Cars where (backwheels+frontwheels)=4;

This error is caused because your table doesn't have a column "totalwheels" and your where clause is trying to run a condition on a column that doesn't exist.

You can try using -

select (backwheels+frontwheels) as totalwheels from Cars where (backwheels+frontwheels)=4;
指尖上得阳光 2024-10-10 11:30:26

最好尝试使用 Alias:

select (c.backwheels+c.frontwheels) as totalwheels from Cars as c where totalwheels=4;

Better try with Alias:

select (c.backwheels+c.frontwheels) as totalwheels from Cars as c where totalwheels=4;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文