Sql 在货币前面添加美元符号

Sql 在货币前面添加美元符号,sql,phpmyadmin,Sql,Phpmyadmin,输出给我的是苹果的价值,但前面没有美元。我知道我需要在某个地方添加concat函数,但我不知道它去了哪里 我在哪里添加(concat(“$”,)?如果我理解正确,您需要使用连接而不是子查询 您的子查询没有意义 concat函数可以组合多个字符串 CONCAT(str1,str2,…) 我想你可以试试这个 select ( (select price from fruit where name = "apple") * sum( quantity ))) as "value of apples"

输出给我的是苹果的价值,但前面没有美元。我知道我需要在某个地方添加concat函数,但我不知道它去了哪里


我在哪里添加
(concat(“$”,)

如果我理解正确,您需要使用
连接
而不是子查询

您的子查询没有意义

concat
函数可以组合多个字符串

CONCAT(str1,str2,…)

我想你可以试试这个

select ( (select price from fruit where name = "apple") * sum( quantity )))
as "value of apples" from inventory
where fruitID = (select fruitID from fruit where name = "apple");
sqlfiddle:

试试这个

select concat('$',f.price * sum(inv.quantity)) as "value of apples" 
from inventory inv
INNER JOIN fruit f on inv.fruitID = f.fruitID
where f.name = "apple";

子查询的数量远远超过需要的数量

此外,如果
fruit
中的
name
列不是唯一的,则查询可能会抛出“太多行”错误

如果
name='apple'
可以匹配
fruit
中的多行,我们可能需要这样的内容:

select ( concat('$', (select price from fruit where name = "apple") * sum( quantity ))) as "value of apples" from inventory where fruitID = (select fruitID from fruit where name = "apple");
如果我们想要匹配的所有
水果的组合值,那么如下所示:

 SELECT f.fruitid
      , f.name
      , SUM( f.price * i.quantity ) AS `total value`
      , CONCAT('$', SUM( f.price * i.quantity ) ) AS `dollar total value`
   FROM fruit f
   LEFT
   JOIN inventory i
     ON f.fruitid = i.fruitid
  WHERE f.name = 'apple'
  GROUP
     BY f.fruitid 


这并不能解决小数点后两位的格式化问题。如果我们想要一个小数点后两位的格式化值,并包含数千个分隔符,我们可以使用MySQL
FORMAT
函数。

提供一些示例数据和预期结果,这确实很有帮助
 SELECT SUM( f.price * i.quantity ) AS `total value`
      , CONCAT('$', SUM( f.price * i.quantity ) ) AS `dollar total value`
   FROM fruit f
   LEFT
   JOIN inventory i
     ON f.fruitid = i.fruitid
  WHERE f.name IN ('apple','pear','pineapple')