SQL 查询错误 - 需要新的眼光
好的,首先,我正在构建一个查询来搜索以 MySQL 数据库形式提供的 MLS 数据,所以我无法控制数据格式,因此我相信我必须进行大量转换以可管理的形式获取数据。正在抛出 SQL 错误。
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near ' DECIMAL(2, 1)) / .5,
CAST(idx1.full_baths, DECIMAL(2, 1))), DECIMAL(2, 1)) AS b' at line 1
查找错误代码,它会将我发送到保留字页面,但我无法识别任何保留字。
现在是 sql
(所有字段都是原生的 VARCHAR
)
SELECT idx_common.mls_no AS mls_no,
CONCAT_WS(" ", idx_common.street_no, idx_common.street_direction, idx_common.street_name) AS address,
idx_common.city AS city,
idx_common.state AS state,
idx_common.total_sqft AS total_sqft,
idx_common.asking_price AS price,
idx1.bedrooms AS bedrooms,
CAST(
SUM(
(CAST(idx1.half_baths, DECIMAL(2, 1)) / .5),
CAST(idx1.full_bath, DECIMAL(2, 1))
),
DECIMAL(2, 1)
) AS bathrooms,
idx1.residential_prop_type AS type,
"Listing Agent" AS agent
FROM (idx_common)
JOIN idx1 ON idx_common.mls_no = idx1.mls_no
WHERE `idx_common`.`mls_no` = 'query'
OR idx_common.zip LIKE '%query%'
OR idx_common.city LIKE '%query%'
Ok, first, I'm building a query to search MLS data that has been provided in the form of a MySQL database, So I don't have control over the data format, and thus I believe I have to do a lot of casting to get the data in a manageable form. The SQL error is being thown.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near ' DECIMAL(2, 1)) / .5,
CAST(idx1.full_baths, DECIMAL(2, 1))), DECIMAL(2, 1)) AS b' at line 1
Looked up the error code and it sends me to a reserved words page, but I can't identify any reserved words.
and now the sql
(all fields are natively VARCHAR
)
SELECT idx_common.mls_no AS mls_no,
CONCAT_WS(" ", idx_common.street_no, idx_common.street_direction, idx_common.street_name) AS address,
idx_common.city AS city,
idx_common.state AS state,
idx_common.total_sqft AS total_sqft,
idx_common.asking_price AS price,
idx1.bedrooms AS bedrooms,
CAST(
SUM(
(CAST(idx1.half_baths, DECIMAL(2, 1)) / .5),
CAST(idx1.full_bath, DECIMAL(2, 1))
),
DECIMAL(2, 1)
) AS bathrooms,
idx1.residential_prop_type AS type,
"Listing Agent" AS agent
FROM (idx_common)
JOIN idx1 ON idx_common.mls_no = idx1.mls_no
WHERE `idx_common`.`mls_no` = 'query'
OR idx_common.zip LIKE '%query%'
OR idx_common.city LIKE '%query%'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信你根本不需要
SUM
:我还将
/ 0.5
更改为* 0.5
因为它似乎更适合这个询问。如果公寓有
3
个半浴室和2
个全套浴室,则此查询将输出(3 / 2) + 2
=3.5< /code> 浴室。
是你想要的吗?
I believe you don't need
SUM
here at all:I also changed
/ 0.5
to* 0.5
since it seems to be more appropriate for this query.If an apartment has
3
half bathrooms and2
full bathrooms, this query will output(3 / 2) + 2
=3.5
bathrooms.Is it what you wanted?
SUM() 采用单个参数。你有 SUM( Cast(...), Cast(...) )
我认为你的意思是 + ,而不是 SUM
(CAST(idx1.half_baths, DECIMAL(2, 1)) / .5) +
CAST(idx1.full_bath, DECIMAL(2, 1))
SUM 将整个表中列的所有值相加。您只能在 GROUP BY 查询中使用它。
SUM() takes a single argument. You have SUM( Cast(...), Cast(...) )
I think you meant to do +, not SUM
(CAST(idx1.half_baths, DECIMAL(2, 1)) / .5) +
CAST(idx1.full_bath, DECIMAL(2, 1))
SUM adds all values of the column in the whole table. You can only use it in a GROUP BY query.
我相信函数
CAST
适用于AS
,而不是","
。像这样:您需要在所有 CAST 上替换它。
I believe that the function
CAST
works withAS
, not a","
. Like this:You need to replace that on all your CASTs.
确保你的MYSQL版本> 5.0.8。直到此版本,DECIMAL 类型才添加到 CAST 函数中。
Ensure that your MYSQL version is > 5.0.8. The DECIMAL type wasn't added to the CAST function until this version.