[英]MYSQL query average price
I have to calculate the average price of a house in Groningen. 我必须计算格罗宁根州房屋的平均价格。 Though the price is not stored as an number but as a string (with some additional information) and it uses a point ('.') as a thousands separator.
尽管价格不是存储为数字,而是存储为字符串(带有一些其他信息),并且它使用点('。')作为千位分隔符。 Price is stored as 'Vraagprijs' in Dutch.
价格以荷兰语存储为“ Vraagprijs”。
The table results are: 表结果为:
€ 95.000 k.k.
€ 116.500 v.o.n.
€ 115.000 v.o.n.
and goes so on... 等等...
My query: 我的查询:
'$'SELECT AVG(SUBSTRING(value,8,8)) AS AveragePrice_Groningen
FROM properties
WHERE name = 'vraagprijs'
AND EXISTS (SELECT *
FROM estate
WHERE pc_wp LIKE '%Groningen%'
AND properties.woid = estate.id);
The result is: 209.47509187620884 But it has to be: 结果是:209.47509187620884但必须是:
20947509187620,884 20947509187620,884
How can i get this done? 我怎样才能做到这一点?
The AVG(SUBSTRING(value,8,8)) dosent work: AVG(SUBSTRING(value,8,8))剂量工作:
sample 样品
MariaDB [yourSchema]> SELECT *,SUBSTRING(`value`,8,8), SUBSTRING_INDEX(SUBSTRING_INDEX(`value`, ' ', -2),' ',1) FROM properties;
+----+-----------------------+------------------------+----------------------------------------------------------+
| id | value | SUBSTRING(`value`,8,8) | SUBSTRING_INDEX(SUBSTRING_INDEX(`value`, ' ', -2),' ',1) |
+----+-----------------------+------------------------+----------------------------------------------------------+
| 1 | € 95.000 k.k. | 95.000 k | 95.000 |
| 2 | € 116.500 v.o.n. | 116.500 | 116.500 |
| 3 | € 115.000 v.o.n. | 115.000 | 115.000 |
+----+-----------------------+------------------------+----------------------------------------------------------+
3 rows in set (0.00 sec)
MariaDB [yourSchema]>
**change it to ** **将其更改为**
AVG(SUBSTRING_INDEX(SUBSTRING_INDEX(`value`, ' ', -2),' ',1))
Try using a CAST DECIMAL and SPLIT for get the right part of the string 尝试使用CAST DECIMAL和SPLIT获取字符串的正确部分
'$'
SELECT AVG( CAST(SPLIT_STR(value,' ', 2)) AS DECIMAL) AS AveragePrice_Groningen
FROM properties
WHERE name = 'vraagprijs'
AND EXISTS (SELECT *
FROM estate
WHERE pc_wp LIKE '%Groningen%'
AND properties.woid = estate.id);
You entered the data with the .
您使用输入了数据
.
as decimal separator, which is normal in Dutch, but not normal in English where they tend to use the ,
as decimal separator. 作为小数点分隔符,他们倾向于使用这是荷兰人正常,但不正常的英语
,
如小数点分隔符。
Enter the data into you database as 215000.000, etc and you should get normal values as answer. 将数据作为215000.000输入到您的数据库中,依此类推,您应该获得正常值作为答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.