gpt4 book ai didi

mysql - 将字符串中的单词与单元格进行比较

转载 作者:可可西里 更新时间:2023-11-01 08:47:22 25 4
gpt4 key购买 nike

鉴于以下情况:

create table original (
dbString VARCHAR(255),
c_Alfa TINYINT(4) Default 0,
c_Bravo TINYINT(4) default 0,
c_Charlie TINYINT(4) default 0
);

INSERT INTO original (dbString) VALUES
('Charlie Echo Delta',0,0,0),
('Foxtrot Golf Hotel',0,0,0),
('Alfa Oscar Tango',0,0,0),
('Charlie Bravo India',0,0,0);

SET @c_string := 'Alfa Bravo Charlie';

在mysql中是否可以将@c_string中的单词(单词是:Alfa,Bravo和Charlie)与表original中的列dbString进行比较并检查如果单元格中出现一个或多个单词。

结果应该是

+---------------------+------------+------------+------------+
| dbString | c_Alfa | c_Bravo | c_Charlie |
+---------------------+------------+------------+------------+
| Charlie Echo Delta | 0 | 0 | 1 |
| Foxtrot Golf Hotel | 0 | 0 | 0 |
| Alfa Oscar Tango | 1 | 0 | 0 |
| Charlie Bravo India | 0 | 1 | 1 |
+---------------------+------------+------------+------------+

我还创建了一个 fiddle :http://sqlfiddle.com/#!2/8eb2b/1

最佳答案

试试这个

SELECT dbString,
IF(locate('Alfa',dbString), 1, 0) AS c_Alfa,
IF(locate('Bravo',dbString), 1, 0) AS c_Bravo,
IF(locate('Charlie',dbString), 1, 0) AS c_Charlie
FROM original;

检查 SQL Fiddle

关于mysql - 将字符串中的单词与单元格进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24733602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com