mySQL另一个组合查询

时间:2013-08-02 14:13:26

标签: php mysql

我有几个陈述,我似乎无法理解如何结合。

现在,我的代码看起来像这样:

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");
}

我有几列(q_title, q_description, q_tags)已编入索引,但我不确定这些语句是否足够有效。

长话短说,我需要这样的陈述:

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

和本声明:

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

合并成一个声明。另外,如果你这么倾向,请告诉我这是多么低效,并可能建议一个更好的方法?

1 个答案:

答案 0 :(得分: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");
}