简体   繁体   English

比较SQL中两列的问题

[英]Problem with comparing two columns in SQL

What I am trying to do.我正在尝试做的事情。 I want to confirm that IvanIvanov from column A corresponds to IvanIvanov _32_y1988_12 from column B. I do not know how to extract IvanIvanov from column B and to compare it with column A. I have tried several things but unsuccessfully.我想确认 A 列中的IvanIvanov对应于 B 列中的IvanIvanov _32_y1988_12。我不知道如何从 B 列中提取IvanIvanov并将其与 A 列进行比较。我尝试了几件事但没有成功。

Then I want to compare column C y1988_12 with part of column B IvanIvanov_32_ y1988_12 .然后我想将列 C y1988_12与列 B IvanIvanov_32_ y1988_12 的一部分进行比较。

DBMS MariaDB数据库管理系统

Thank you in advance for the help.预先感谢您的帮助。

Table representation:表表示:

A一种 B C C
IvanIvanov伊万·伊万诺夫 IvanIvanov_32_y1988_12 IvanIvanov_32_y1988_12 y1988_12 y1988_12

You wonder how to extract the two substrings from B. You don't have to.您想知道如何从 B 中提取两个子字符串。您不必这样做。 You merely want to know whether B starts or ends with the substrings A and C. The easiest aproach here is probably LIKE .您只想知道 B 是以子串 A 和 C 开头还是结尾。这里最简单的方法可能是LIKE I am assuming your DBMS is MySQL or MariaDB for the mentioned function SUBSTRING_INDEX .我假设您的 DBMS 是 MySQL 或 MariaDB,用于上述函数SUBSTRING_INDEX

With MySQL's CONCAT function:使用 MySQL 的CONCAT函数:

select
  b like concat(a, '%') as b_starts_with_a,
  b like concat('%', c) as b_ends_with_c
from mytable;

Or with the standard SQL concatenation operator:或者使用标准的 SQL 连接运算符:

select
  b like a || '%' as b_starts_with_a,
  b like '%' || c as b_ends_with_c
from mytable;

This gives you true / 1 in case of a match and false / 0 in case of a mismatch这在匹配的情况下为您提供 true / 1,在不匹配的情况下为您提供 false / 0

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

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