SQL 查询错误 - 需要新的眼光

发布于 2024-10-12 08:49:12 字数 1298 浏览 8 评论 0原文

好的,首先,我正在构建一个查询来搜索以 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 技术交流群。

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

发布评论

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

评论(4

输什么也不输骨气 2024-10-19 08:49:12

我相信你根本不需要 SUM

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(idx1.half_baths AS DECIMAL(2, 1)) * .5 +
        CAST(idx1.full_bath AS 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%'

我还将 / 0.5 更改为 * 0.5 因为它似乎更适合这个询问。

如果公寓有 3 个半浴室和 2 个全套浴室,则此查询将输出 (3 / 2) + 2 = 3.5< /code> 浴室。

是你想要的吗?

I believe you don't need SUM here at all:

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(idx1.half_baths AS DECIMAL(2, 1)) * .5 +
        CAST(idx1.full_bath AS 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%'

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 and 2 full bathrooms, this query will output (3 / 2) + 2 = 3.5 bathrooms.

Is it what you wanted?

情深缘浅 2024-10-19 08:49:12

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.

梦途 2024-10-19 08:49:12

我相信函数 CAST 适用于 AS,而不是 ","。像这样:

CAST(idx1.half_baths AS DECIMAL(2, 1))

您需要在所有 CAST 上替换它。

I believe that the function CAST works with AS, not a ",". Like this:

CAST(idx1.half_baths AS DECIMAL(2, 1))

You need to replace that on all your CASTs.

鹿! 2024-10-19 08:49:12

确保你的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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文