dharminder singh
dharminder singh

Reputation: 1

how to search between two column

Given a table with the following columns and data:

comment, minAge, maxAge
"a"      1       5
"b"      13      25
"c"      20      50
"d"      22 

Is it possible to do a sql query like:

select * from rows where 16 to 22

between minAge and maxAge. With the results:

"b", 13, 19
"c" ,20, 50
"d" ,22

Upvotes: 0

Views: 48

Answers (2)

David Jones
David Jones

Reputation: 4305

You can do something like this:

SELECT * FROM `table` WHERE `minAge` >= 16 AND `maxAge` <= 22

This would get every row where the minAge column is greater than or equal to 16 and the maxAge column is less than or equal to 22.

UPDATE

If you want to also return rows where minAge and/or maxAge is null use this.

SELECT * FROM `table` WHERE (`minAge` >= 16 || `minAge` IS NULL) AND (`maxAge` <= 22 || `maxAge` IS NULL)

So this query should now satisfy the row where the comment has a value of "d". Credit to Tom Lord for the suggestion.

Upvotes: 2

Jos&#233; Miguel
Jos&#233; Miguel

Reputation: 11

You can use BETWEEN Sql condition

SELECT * FROM table
WHERE column BETWEEN value1 AND value2;

You can view specific information in this link

Upvotes: 0

Related Questions