0

I have a couple of statements that I can't seem to understand how to combine.

Right now, my code looks like this:

if(!$question && $filter){
    $qry = mysql_query("SELECT * FROM mbr_qa_questions WHERE q_tags RLIKE '[[:<:]]" . $filter . "[[:>:]]' ORDER BY q_id ASC LIMIT $start, 1 ");
} else if($question && !$filter){
    $qry = mysql_query("SELECT * FROM mbr_qa_questions WHERE MATCH (q_title, q_description, q_tags) AGAINST ('$question') ORDER BY q_id ASC LIMIT $start, 1 ");
} else if($question && $filter){
    //NEED A COMBINED QUERY OF THE FIRST TWO HERE.
} else {
    $qry = mysql_query("SELECT * FROM mbr_qa_questions ORDER BY q_id ASC LIMIT $start, 1");
}

I have the few columns (q_title, q_description, q_tags) indexed, though I'm not sure any of these statements are efficient enough.

Long story short, I need this statement:

SELECT * FROM mbr_qa_questions WHERE q_tags RLIKE '[[:<:]]" . $filter . "[[:>:]]' ORDER BY q_id ASC LIMIT $start, 1

and this statement:

SELECT * FROM mbr_qa_questions WHERE MATCH (q_title, q_description, q_tags) AGAINST ('$question') ORDER BY q_id ASC LIMIT $start, 1

combined into a single statement. Also, if you're so inclined, please tell me how inefficient this is and possibly suggest a better way?

4

1 回答 1

1

使用 AND 运算符

if(!$question && $filter){
    $qry = mysql_query("SELECT * FROM mbr_qa_questions WHERE q_tags RLIKE '[[:<:]]" . $filter . "[[:>:]]' ORDER BY q_id ASC LIMIT $start, 1 ");
} else if($question && !$filter){
    $qry = mysql_query("SELECT * FROM mbr_qa_questions WHERE MATCH (q_title, q_description, q_tags) AGAINST ('$question') ORDER BY q_id ASC LIMIT $start, 1 ");
} else if($question && $filter){
    //combined using AND 
    $qry = mysql_query("SELECT * FROM mbr_qa_questions WHERE MATCH (q_title, q_description, q_tags) AGAINST ('$question') AND q_tags RLIKE '[[:<:]]" . $filter . "[[:>:]]' ORDER BY q_id ASC LIMIT $start,     1 ");
} else {
    $qry = mysql_query("SELECT * FROM mbr_qa_questions ORDER BY q_id ASC LIMIT $start, 1");
}
于 2013-08-02T14:32:30.223 回答