简体   繁体   中英

Match value with alternate position

I want to self join a table on a column having value with alternate position, specifying the position is easy for one record, but I have thousands of record with different values. I'm not sure how to do it generally. I tried to match it by palindrome using reverse() but of course it's not the way to do it. I have a value like below:

Block Range
A 1-10
A 10-1

I want to match the value as if 1-10=10-1 in general way.

As @Akina mentions, I tried parsing the table data with following query:

SELECT t.`Block`, 
LEAST(SUBSTRING_INDEX(t.`Range`,'-',1) ,SUBSTRING_INDEX(t.`Range`,'-',-1)) leastNumber,
GREATEST(SUBSTRING_INDEX(t.`Range`,'-',1) ,SUBSTRING_INDEX(t.`Range`,'-',-1)) greatestNumber
 FROM test_table t

which will give you results

Block leastNumber greatestNumber
A 1 10
A 1 10

And then may be you can join them with ease.

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