简体   繁体   中英

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.

Then I want to compare column C y1988_12 with part of column B IvanIvanov_32_ y1988_12 .

DBMS MariaDB

Thank you in advance for the help.

Table representation:

A B C
IvanIvanov IvanIvanov_32_y1988_12 y1988_12

You wonder how to extract the two substrings from B. You don't have to. You merely want to know whether B starts or ends with the substrings A and C. The easiest aproach here is probably LIKE . I am assuming your DBMS is MySQL or MariaDB for the mentioned function SUBSTRING_INDEX .

With MySQL's CONCAT function:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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