如何搜索列值

发布于 2024-12-21 01:32:35 字数 628 浏览 0 评论 0原文

表 1

Values

9A 200x400x600
10B 400x200x500
....

值列数据类型为 nvarchar

查询

Select * from table1 where values like '" & textbox1.text & "%'

上述查询有效,但我想以 4 种方式进行搜索

我想用每 3 位数字搜索一列

查询不应检查 spacex< /code> 列中的值。

预期输出

  • 如果我输入 textbox1 9A 或 10A,查询应返回前 2 或 3 个值
  • 如果我输入 textbox2 200 或 400,查询应从第一行返回 200 或从第二行返回 400
  • 如果我输入 textbox3 400 或 200 查询应该从第一行返回 400 或从第二行返回 200
  • 如果我在 textbox4 中输入 600 或 500 查询应从第一行返回 600,从第二行返回 500

如何拆分搜索的列值

需要查询帮助

Table1

Values

9A 200x400x600
10B 400x200x500
....

Values Column datatype is nvarchar

Query

Select * from table1 where values like '" & textbox1.text & "%'

The above query is working, but I want to search in 4 ways

I want to search a column with every 3 digit

The query should not check space and x values from the column.

Expected Output

  • if I type in textbox1 9A or 10A the query should return first 2 or 3 values
  • If I type in textbox2 200 or 400 the query should return 200 from first row or 400 from second row
  • If I type in textbox3 400 or 200 the query should return 400 from first row or 200 from second row
  • If I type in textbox4 600 or 500 the query should return 600 from first row and 500 from second row

How to split the column value for search

Need query help

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

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

发布评论

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

评论(3

杯别 2024-12-28 01:32:35

如您所知,该查询是一个有点混乱的问题。我会尝试,让我知道这是否接近您正在寻找的。

我假设:

9A 200x400x600  -> 200x400x600 are de 3*2+2 lasts characters.
10B 400x200x500 -> CHARINDEX(' ', value ) = 4

那么,查询是:

Select 
     * 
from table1 
where 
   substring( values, 1, CHARINDEX(' ', value ) ) = '" & textbox1.text & "' OR
   substring( right( value, 3*2+2), 1, 3 ) = '" & textbox2.text & "' OR
   substring( right( value, 3*2+2), 5, 3 ) = '" & textbox3.text & "' OR
   substring( right( value, 3*2+2), 9, 3 ) = '" & textbox4.text & "' 

已编辑:

select * from
   (Select 
     substring( values, 1, CHARINDEX(' ', value ) ) as split1,
     substring( right( value, 3*2+2), 1, 3 ) as split2,
     substring( right( value, 3*2+2), 5, 3 ) as split3,
     substring( right( value, 3*2+2), 9, 3 ) as split4,
     * 
   from table1 ) SplitedTable
where 
   SplitedTable.split1 = '" & textbox1.text & "' OR
   SplitedTable.split2 = '" & textbox2.text & "' OR
   SplitedTable.split3 = '" & textbox3.text & "' OR
   SplitedTable.split4 = '" & textbox4.text & "' 

As you know the query is a the question is a bit confused. I will try, let me know if this is close that you are looking for.

I assume that:

9A 200x400x600  -> 200x400x600 are de 3*2+2 lasts characters.
10B 400x200x500 -> CHARINDEX(' ', value ) = 4

Then, the query is:

Select 
     * 
from table1 
where 
   substring( values, 1, CHARINDEX(' ', value ) ) = '" & textbox1.text & "' OR
   substring( right( value, 3*2+2), 1, 3 ) = '" & textbox2.text & "' OR
   substring( right( value, 3*2+2), 5, 3 ) = '" & textbox3.text & "' OR
   substring( right( value, 3*2+2), 9, 3 ) = '" & textbox4.text & "' 

edited:

select * from
   (Select 
     substring( values, 1, CHARINDEX(' ', value ) ) as split1,
     substring( right( value, 3*2+2), 1, 3 ) as split2,
     substring( right( value, 3*2+2), 5, 3 ) as split3,
     substring( right( value, 3*2+2), 9, 3 ) as split4,
     * 
   from table1 ) SplitedTable
where 
   SplitedTable.split1 = '" & textbox1.text & "' OR
   SplitedTable.split2 = '" & textbox2.text & "' OR
   SplitedTable.split3 = '" & textbox3.text & "' OR
   SplitedTable.split4 = '" & textbox4.text & "' 
深居我梦 2024-12-28 01:32:35

请研究数据库规范化,特别是第一范式

目前,您的数据字段 values 用于在一行中保存多个不同的值 - 例如。 9A 200x400x600。 (不仅如此,您还在同一字段中使用不同的值分隔符,因为 x 都用于分隔字段。)

SQL 不是为此设计的某种操纵。

虽然可以生成返回相关值的查询,但这样的查询会很笨重且不灵活。因此我建议规范化数据结构,以更适合查询。

Please look into database normalisation, particularly first normal form.

At present, your data field values is being used to hold multiple different values in a single row - eg. 9A 200x400x600. (Not only that, but you are also using different value delimiters in the same field, as both and x are being used to delimit fields.)

SQL is not designed for this sort of manipulation.

While it would be possible to produce a query that would return the relevant values, such a query would be unwieldy and inflexible. I therefore suggest normalising the data structure, to be more suitable for querying.

夜巴黎 2024-12-28 01:32:35
Select " & textbox1.text & " as mYSearch,* 
from table1 
where values like '%" & textbox1.text & "%'
Select " & textbox1.text & " as mYSearch,* 
from table1 
where values like '%" & textbox1.text & "%'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文