SQL 大于子字符串

发布于 2024-10-20 03:15:11 字数 423 浏览 1 评论 0原文

我有一个 SQL 字段,如下所示:

          ROOMS
=======================
bedrooms 2, bathrooms 2
bedrooms 3, bathrooms 2
bedrooms 6, bathrooms 1
bedrooms 1, bathrooms 4
bedrooms 1, bathrooms 2
bedrooms 4, bathrooms 4
bedrooms 1, bathrooms 3

如何形成单个 SELECT 语句来检索卧室数大于 3 的所有行?

另外,这个语句是否可以改变以检索浴室大于 3 的所有行?

编辑

是的,是的,是的,可怕的数据系统。 :)

然而,从概念上讲,有没有办法实现这一点?

I have a single SQL field like so:

          ROOMS
=======================
bedrooms 2, bathrooms 2
bedrooms 3, bathrooms 2
bedrooms 6, bathrooms 1
bedrooms 1, bathrooms 4
bedrooms 1, bathrooms 2
bedrooms 4, bathrooms 4
bedrooms 1, bathrooms 3

How can I form a single SELECT statement to retrieve all rows with bedrooms greater than 3?

Also, can this statement can be mutated to retrieve all rows with bathrooms greater than 3?

EDIT

Yes, yes, yes, horrible data system. :)

Conceptually, however, is there a way to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

最初的梦 2024-10-27 03:15:11

首先,我希望我不必指出这是一个多么可怕的数据模型。

解决了这个问题:

您只需解析字段中的数据即可。如果格式始终如上,您可能会使用 substr,如:(

SELECT * FROM table WHERE SUBSTR(BEDROOMS, 9,1)>3

不保证数字:-))。

如果格式更灵活(例如超过9个床/浴室,首先给出浴室),您可能需要使用正则表达式来解析。大多数 DBMS 支持正则表达式,请参见 http://www.postgresql.org /docs/8.3/static/functions-matching.html

First, I hope I don't have to point out what a horrible, horrible data model this is.

With that out of the way:

You'll just have to parse the data in the field. If the format is always like above, you might get away with using substr, as in :

SELECT * FROM table WHERE SUBSTR(BEDROOMS, 9,1)>3

(no guarantee for the numbers :-) ).

If the format is more flexible (e.g. more than 9 bed-/bathrooms, bathrooms given first), you'll probably need to use a regular expression to parse. Most DBMS support regular expressions, see e.g. http://www.postgresql.org/docs/8.3/static/functions-matching.html

谷夏 2024-10-27 03:15:11

好吧,这就是“发明”规范化的原因,这样您就不会得到像这样的蹩脚模式。无论如何,无论是谁设计的,都应该是......。

我建议首先修复架构,然后这不会成为问题,但要回答您的问题,您可以将所有出现的“卧室”替换为“”,然后将所有出现的“浴室”替换为说“-”,然后从那里形成一个查询。

SELECT CONVERT(int, SUBSTRING(ButcheredColumn, CHARINDEX('-', ButcheredColumn))) AS NumBedrooms
, CONVERT(int, RIGHT(ButcheredColumn, CHARINDEX('-', ButcheredColumn))) AS NumBathrooms
FROM (
    SELECT REPLACE(REPLACE(ROOMS, 'bedrooms ',''), ', bathrooms ', '-') AS ButcheredColumn
    FROM CrappyDesignedTable1
) AS ButhceredTable

可能需要稍微使用 charindex 才能获得正确的列,然后您可以使用该表作为子查询并从中进行选择。我建议如果你不知道什么是子查询,就放下你所有的工具,去你的 iPhone 上玩愤怒的小鸟吧。

Well, this is why normalization was 'invented' so that you won't end up with crappy schemas like this. WHoever designed this should be ...., anyways.

I suggest firstly to fix up the schema, and then this won't be a problem, but to answer your question, you can replace all occurences of the "bedrooms " with "" and then replace all ocurrences of the ", bathrooms " with say "-", then from there form a query.

SELECT CONVERT(int, SUBSTRING(ButcheredColumn, CHARINDEX('-', ButcheredColumn))) AS NumBedrooms
, CONVERT(int, RIGHT(ButcheredColumn, CHARINDEX('-', ButcheredColumn))) AS NumBathrooms
FROM (
    SELECT REPLACE(REPLACE(ROOMS, 'bedrooms ',''), ', bathrooms ', '-') AS ButcheredColumn
    FROM CrappyDesignedTable1
) AS ButhceredTable

might have to play with charindex a little to get the right column, then you can use this table as a subquery and select from it. And I suggest if you dont know what a subquery is, to put all your tools down and go play angry birsds on your IPhone.

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