doulu7921 2012-07-03 15:57
浏览 14
已采纳

如何在许多表中使用关键字进行搜索

i have three tables in my database book,author,publishing ... i want the user can to search whithin the three tables if he write any word related . the same word if not found in the book table it will search in the another table and so on...i do it but some thing wrong and i want it in one mysql sentence..anyone help me please

function getsomeword($keyword) {
    $result1 = $this->db->query("SELECT bo_id,bo_name,bo_state,bo_about FROM d_book where (bo_name like '%$keyword%' or bo_about like '%$keyword%') and bo_state = '1'");
    $result1 = $result1->num_rows();
    $result2 = $this->db->query("SELECT au_id,au_name,au_state,au_info FROM d_author where (au_name like '%$keyword%' or au_info like '%$keyword%') and au_state = '1'");
    $result2 = $result2->num_rows();
    $result3 = $this->db->query("SELECT pub_id,pub_name,pub_state,pub_info FROM d_publishing where (pub_name like '%$keyword%' or pub_info like '%$keyword%') and pub_state = '1'");
    $result3 = $result3->num_rows();
    return $result1 + $result2 + $result3;

}
  • 写回答

1条回答 默认 最新

  • dongtangu8615 2012-07-03 16:03
    关注

    You're looking for the UNION keyword :

    SELECT ...
    UNION [ALL | DISTINCT] SELECT ...
    [UNION [ALL | DISTINCT] SELECT ...]
    

    http://dev.mysql.com/doc/refman/5.0/en/union.html

    Do NOT forget to sanitize your inputs, seems like you're putting the $keyword value in your query without escaping/sanitizing it before. You should look into PDO::prepare to prevent SQL injection (http://www.php.net/manual/fr/pdo.prepare.php).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch