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).

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

报告相同问题?

悬赏问题

  • ¥15 php 将rtmp协议转hls协议,无法播放
  • ¥15 miniconda安装不了
  • ¥20 python代码编写
  • ¥20 使用MPI广播数据遇到阻塞
  • ¥15 TinyMCE如何去掉自动弹出的“链接…”工具?
  • ¥15 微信支付转账凭证,如何解决
  • ¥15 在win10下使用指纹登录时,界面上的文字最后一个字产生换行现象
  • ¥20 使用AT89C51微控制器和MAX7219驱动器来实现0到99秒的秒表计数,有开始和暂停以及复位功能,下面有仿真图,请根据仿真图来设计c语言程序
  • ¥15 51单片机 双路ad同步采样
  • ¥15 使用xdocreport 生成word