0

I am not well versed in MYSQL, and I need some guidance here. I have read multiple MYSQL WHERE statement explanations, but have not found this exact issue. I have a table called studies that has fields like id, name, author, category, format and a few others.

I am building a filter with checkboxes to narrow down the query. I want to get all studies with a specific author AND category AND format. My problem comes when I build the query. If only the format is selected, the query returns empty because no author was selected. How do I get the results from the AND statements without the WHERE statement? Here is what I have so far...

"SELECT * FROM estudos WHERE subcategory='$subcategory' AND author='$author' AND format='$format'"

7 Answers 7

1

If author is empty, don't include it in the query:

SELECT * FROM estudos WHERE format='$format'

Alternatively, use OR instead of AND:

SELECT * FROM estudos WHERE subcategory='$subcategory' OR author='$author' OR format='$format'
Sign up to request clarification or add additional context in comments.

5 Comments

So you are saying that I should build multiple queries in php with if statements, right? The problem with OR is that it will return broader results. I want more specific results.
@SableFoste That depends on what behavior he wants when both author and format are specified in the search.
@JeremyTyler Yes. You can simply check for each field being empty and append it to the query if it isn't, or do something like the other answer: stackoverflow.com/a/19815053/1324019
@Mansfield Thank you. All these answers are helpful. Now to the test to see which works best.
@JeremyTyler You're welcome. I'd recommend the answer I previously linked, it's the most flexible.
1

You need to conditionally append the conditions based on whether the item was selected or not. I suggest something like:

$where = array();
$params = array();
if (isset($format)) {
    $where[] = "format = ?";
    $params[] = $format;
}
/* other parameters */
$query = "SELECT * FROM estudos WHERE "
    . implode(' AND ', $where);

Note that the $params part is only necessary if you are using a parameterized query like with PDO (which you should be doing).

Comments

0

if only format is selected you should only query based on format like

SELECT * FROM estudos WHERE format='$format'

if format and author is selected then

SELECT * FROM estudos WHERE author='$author' AND format='$format'

if all are selected then

SELECT * FROM estudos WHERE subcategory='$subcategory' AND author='$author' AND format='$format'

You should run your query depending on the selection

Comments

0

assign where condition to variable

for example

if($categoryCheckbox){
 $whare.="AND category='$category'";
}

and append it to your query

Comments

0

You could use he following code to generate the query

$q = "SELECT * FROM estudos WHERE format='$format'";
if ($author != '') { $q.= "AND author='$author'"; }
if ($subcategory != '') { $q.= "AND subcategory='$subcategory'"; }

Comments

0

Well what i would do is this:

$query = sprintf("SELECT * FROM estudos");
if ($subcategory != "" || $author != "" || $format != "") {
    $query .= " WHERE ";
    $i = 0;

    if ($subcategory != "") {
        if ($i > 0) $query .= " AND ";
        $query .= "subcategory='" . $subcategory . "'";
    }
    if ($author != "") {
        if ($i > 0) $query .= " AND ";
        $query .= "author='" . $author . "'";
    }
    if ($format != "") {
        if ($i > 0) $query .= " AND ";
        $query .= "format='" . $format . "'";
    }
}

I hope that works.

Comments

0

I will suggest you to rewrite the query by using concatenation. Like:

$query= 'SELECT * FROM estudos WHERE 1';

if(isset($subcategory)) $query .= ' AND subcategory='.$subcategory;

if(isset($author)) $query .= ' AND author='.$author;

if(isset($format)) $query .= ' AND format='.$format;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.