简体   繁体   English

使用计算在查询中返回 2 个额外的列

[英]return 2 extra columns on query with calculation

What I want to do is probably very simple, but I have a table:我想做的可能很简单,但我有一张桌子:

|---------------------|------------------|
|          ID         |       price      |
|---------------------|------------------|
|          1          |         30       |
|---------------------|------------------|

Which query I am using in this procedure:我在此过程中使用了哪个查询:

PROCEDURE `get_info`(IN itemID VARCHAR(255), IN itemQuantity VARCHAR(255)) 
BEGIN
SELECT * from table where ID = itemID;
END

Now I would like the query to return 2 extra columns, with "quantity", which is procedures input "itemQuantity" and the extra column "totalPrice" which is itemQuantity*price.现在我希望查询返回 2 个额外的列,“数量”是过程输入“itemQuantity”,额外的列“totalPrice”是 itemQuantity*price。

So the table looks like this:所以表格看起来像这样:

|---------------------|------------------|----------------------|------------------|
|          ID         |       price      |       quantity       |     totalPrice   |
|---------------------|------------------|----------------------|------------------|
|          1          |         30       |          2           |        60        |
|---------------------|------------------|----------------------|------------------|

I can return everything separately, but how do I combine it into one query result?我可以单独返回所有内容,但如何将其合并为一个查询结果?

Is this what you want?这是你想要的吗?

PROCEDURE `get_info`(
    IN in_itemID VARCHAR(255),
    IN in_itemQuantity VARCHAR(255)) 
BEGIN
    SELECT t.*, in_itemQuantity as itemQuantity, in_itemQuantity * t.price as totalprice
    FROM table t
    WHERE t.ID = in_itemID;
END;

Notice that I changed the names of the parameters, by prefixing them with in_ .请注意,我通过在参数前加上in_更改参数的名称。 This significantly reduces the chance that the parameter conflicts with an existing column name.这显着降低了参数与现有列名冲突的可能性。

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

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