SQL 大于子字符串
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我希望我不必指出这是一个多么可怕的数据模型。
解决了这个问题:
您只需解析字段中的数据即可。如果格式始终如上,您可能会使用
substr
,如:(不保证数字:-))。
如果格式更灵活(例如超过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 :(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
好吧,这就是“发明”规范化的原因,这样您就不会得到像这样的蹩脚模式。无论如何,无论是谁设计的,都应该是......。
我建议首先修复架构,然后这不会成为问题,但要回答您的问题,您可以将所有出现的“卧室”替换为“”,然后将所有出现的“浴室”替换为说“-”,然后从那里形成一个查询。
可能需要稍微使用 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.
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.