Elumalai Kaliyaperumal
Elumalai Kaliyaperumal

Reputation: 1520

How to get non-common value by comparing two values in SELECT query mysql?

For example I have a SELECT query that returning result of two columns called ordered_zones and available_zone. Both results zone name like below

Original Result

Expected result is result set to have one more column called non_matching_zone. This column value should come from ordered_zones by matching with available_zone that is non-common values like below

enter image description here

Can I achieve above result with query itself?

Upvotes: 1

Views: 53

Answers (2)

Elumalai Kaliyaperumal
Elumalai Kaliyaperumal

Reputation: 1520

Thank you all for your answers. My problem gets solved with mysql FIND_IN_SET method by IF(NOT FIND_IN_SET(ordered_zone, available_zones), ordered_zone, "") AS non_matching_zone.

Upvotes: 1

Hasaan Mubasher
Hasaan Mubasher

Reputation: 160

Assuming available_zone always contain only one zone, you can replace available_zone value in ordered_zones with empty string

SELECT ordered_zones, available_zone, REPLACE(ordered_zones, available_zone, '') non_matching_zone
FROM table

Little work around is needed with commas, but that's the idea.

Upvotes: 0

Related Questions