MySQL query with optional filter

almo

I my table I have one column 'x' that is either 1 or 0. I need a MySQL query that returns either all rows, or those where x = 1 or where x = 0.

Which one of those threee results I want is set in a variable $type. What I did is create three queries and a if clause:

if ($type == 'all') $query = "SELECT * FROM contracts";
elseif ($type == 'n') $query = "SELECT * FROM contracts WHERE x = '0'";
elseif ($type == 'f') $query = "SELECT * FROM contracts WHERE x = '1'";

This works but I does not seem the best solution to me because I have to write 3 times an almost same query and if I want to change something I have to change it 3 times.

I tried to make $type = '', 0 or 1 and then WHERE x = '$type' but that did not work.

How would I do this using only one MySQL Query?

Barmar
if ($type = 'all') {
    $filter = '';
} elseif ($type == 'n') {
    $filter = "WHERE x = '0'";
} elseif ($type == 'f') {
    $filter = "WHERE x = '1'";
} else {
    die("Invalid filter type: $type");
}
$query = "SELECT * FROM contracts $filter";

If you use the scheme where $type = '', 0, or 1, you can do:

$query = "SELECT * FROM contracts";
if ($type !== '') {
    $query .= " WHERE x = '$type'";
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring JPA filter optional criteria in Query method

From Dev

MySQL filter query?

From Dev

MySql filter query

From Dev

How to filter query in MySQL

From Dev

Mysql query to filter result

From Dev

Mysql Query issue with join and filter

From Dev

Mysql filter query where condition

From Dev

Filter MYSQL query with form options

From Dev

Filter a mysql query using php

From Dev

SELECT query to filter records using optional user-supplied parameters

From Dev

How to filter a determined quantity of rows in a mysql query?

From Dev

Need a MySQL query that can filter the results

From Dev

MySQL query for search filter from multiple tables

From Dev

Filter rows in a query using HAVING in MySQL

From Dev

Filter MySQL Query By DateTime Stored As Varchar

From Dev

PHP Mysql query - allow user to filter results

From Dev

MySQL query for search filter from multiple tables

From Dev

filter rows based on column values in mysql query

From Dev

Filter rows in a query using HAVING in MySQL

From Dev

PHP Query To Filter Many-To-Many MySQL

From Dev

Adjust mysql database query to filter column value

From Dev

Mysql/Php query grouping filter result

From Dev

Filter a mysql query with conditions / Group the result

From Dev

How to filter a determined quantity of rows in a mysql query?

From Dev

Optimize MySQL query with range filter on related table

From Dev

mysql query to filter by multiple coordinates boundaries

From Dev

Jena sparql (Dbpedia) query OPTIONAL filter gives no results but (http://dbpedia.org/snorql/) same query works

From Dev

how to filter for an optional field

From Dev

Optional parameters in SQL query

Related Related

HotTag

Archive