简体   繁体   English

如何在包含来自SQL中另一个单元格的字符串的单元格子字符串中搜索字符串

[英]How do I search for a string in a cell substring containing a string from another cell in SQL

I am looking to compare the results of 2 cells in the same row. 我希望比较同一行中2个单元格的结果。 the way the data is structured is essentially this: 数据的结构方式基本上是这样的:

Col_A: table,row,cell   
Col_B: row

What I want to do is compare when Col_A 'row' is the same as Col_B 'row' 我想做的是在Col_A'row'与Col_B'row'相同时进行比较

SELECT COUNT(*) FROM MyTable WHERE Col_A CONTAINS Col_B;

sample data: 样本数据:

Col_A: a=befd-47a8021a6522,b=7750195008,c=prof    
Col_B: b=7750195008

Col_A: a=bokl-e5ac10085202,b=4478542348,c=pedf
Col_B: b=7750195008

I am looking to return the number of times the comparison between Col_A 'b' and Col_B 'b' is true. 我想返回Col_A'b'和Col_B'b'之间比较的次数为真。

这就是我想要的:

SELECT COUNT(*) FROM MyTable WHERE Col_A LIKE CONCAT('%',Col_B,'%');

I see You answered Your own question. 我看到您回答了您自己的问题。

SELECT COUNT(*) FROM MyTable WHERE Col_A LIKE CONCAT('%',Col_B,'%');

is good from performance perspective. 从性能角度来看是好的。 While normalization is very good idea, it would not improve speed much in this particular case. 虽然规范化是一个好主意,但在这种特殊情况下,它不会大大提高速度。 We must simply scan all strings from table. 我们必须简单地扫描表中的所有字符串。 Question is, if the query is always correct. 问题是,查询是否始终正确。 It accepts for example 它接受例如

Col_A: a=befd-47a8021a6522,ab=7750195008,c=prof    
Col_B: b=7750195008

or 要么

Col_A: a=befd-47a8021a6522,b=775019500877777777,c=prof    
Col_B: b=7750195008

this may be a problem depending on the data format. 根据数据格式,这可能是一个问题。 Solution is quite simple 解决方法很简单

SELECT COUNT(*) FROM MyTable WHERE CONCAT(',',Col_A,',') LIKE CONCAT('%,',Col_B,',%');

But this is not the end. 但这还没有结束。 String in LIKE is interpreted and if You can have things like % in You data You have a problem. LIKE中的字符串会被解释,并且如果您可以在数据中包含%之类的内容,则说明存在问题。 This should work on mysql: 这应该适用于mysql:

SELECT COUNT(*) FROM MyTable WHERE LOCATE(CONCAT(',',Col_B,','), CONCAT(',',Col_A,','))>0;
SELECT * FROM MyTable WHERE Col_A = Col_B (AND Col_A = 'cell')

^^ Maybe you are looking for this statement. ^^也许您正在寻找此声明。 The part in brackets is optional. 括号中的部分是可选的。 If this is not the solution, please supply us with further information. 如果这不是解决方案,请向我们提供更多信息。

If column Col_a has data with format table,row,cell then search expression will be next: 如果列Col_a具有表,行,单元格格式的数据则搜索表达式将是下一个:

SELECT COUNT(*) FROM MyTable AS MT
WHERE SUBSTRING(Col_A, 
                INSTR(Col_A, ',b=') + 3, 
                INSTR(Col_A, ',c=') - INSTR(Col_A, ',b=') + 3) = Col_B

The easiest way would be to use the IN operator. 最简单的方法是使用IN运算符。

SELECT COUNT(*) FROM MyTable WHERE Col_A IN (Col_B);

More info on the IN operator: http://www.w3schools.com/sql/sql_in.asp 有关IN运算符的更多信息: http : //www.w3schools.com/sql/sql_in.asp

There's also the SUBSTRING() or MID() (depending on what you're using) function if you know that the substring will be in the same position everytime. 如果您知道子字符串每次都位于同一位置,则还有SUBSTRING()MID() (取决于您使用的功能)函数。

MID()/SUBSTRING() function: http://www.w3schools.com/sql/sql_func_mid.asp MID()/ SUBSTRING()函数: http//www.w3schools.com/sql/sql_func_mid.asp

You can use SUBSTRING_INDEX to extract a delimited field from a column. 您可以使用SUBSTRING_INDEX从列中提取定界字段。

SELECT COUNT(*)
FROM MyTable
WHERE Col_B = SUBSTRING_INDEX(SUBSTRING_INDEX(Col_A, ',', 2), ',', -1)

You need to call it twice to get a single field. 您需要调用两次才能得到一个字段。 The inner call gets the first two fields, the outer call gets the last field of that. 内部调用获取前两个字段,外部调用获取该字段的最后一个字段。

Note that this will be very slow if the table is large, because it's not possible to index substrings in MySQL. 请注意,如果表很大,这将非常慢,因为在MySQL中无法索引子字符串。 It would be much better if you normalized your schema so each field is in a separate column. 如果规范化架构,使每个字段位于单独的列中,那就更好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM