---
title: "Select by pair of fields in MySQL"  
description: "Select by pair of fields in MySQL"  
author: "Anonymous User"  
published: 2013-05-18  
updated: 2013-05-18  
canonical: https://www.mindstick.com/forum/903/select-by-pair-of-fields-in-mysql  
category: "mssql server"  
tags: ["mssql server"]  
reading_time: 1 minute  

---

# Select by pair of fields in MySQL

Hi [Expert](https://www.mindstick.com/articles/13120/an-expert-financial-advice-will-improve-your-finances)!\
I have the following [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap):\
+-------+-------+-------------+|column1 |column2| column3 |\
| 2 | 4 | [row](https://www.mindstick.com/forum/1212/this-row-already-belongs-to-this-table) 1 | < [compare](https://www.mindstick.com/forum/2036/propertyinfo-getvalue-unable-to-compare-datetimes) with this| 4 | 3 | row 2 | + < here`s 4| 5 | 2 | row 3 | + < here`s 2| 1 | NULL | row 4 | - < no 4 or 2| 5 | 6 | row 5 | - < no 4 or 2| NULL | 2 | row 6 | + < here`s 2\
The [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is how to find all rows, that contain at [least](https://yourviews.mindstick.com/story/1471/5-autobiographies-you-should-read-at-least-once) one searched value. For example I need to find rows like first, so I`m looking for rows with 2 or 4 in \
first two columns. So my [output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular) should be rows 2,3 and 6. I do not need to find [NULL values](https://www.mindstick.com/interview/34072/optional-class-in-java-handling-null-values-more-effectively).\
Please advise.Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)!

## Replies

### Reply by AVADHESH PATEL

Hi Goti!\
Try as following\
select * from foo where ( 2 in (col1, col2) or 4 in (col1, col2)) and (least(col1,col2), greatest(col1,col2)) <> (2,4)\
I hope it helpful for you.\


---

Original Source: https://www.mindstick.com/forum/903/select-by-pair-of-fields-in-mysql

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
