我的数据库书中有三个表,作者,发布...我希望用户可以在三个表中搜索任何相关的单词。如果在书表中找不到相同的单词,它将在另一个表中搜索,依此类推......我这样做但有些不对劲,我想在一个mysql语句中找到它。有人请帮助我
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;
}
答案 0 :(得分:2)
您正在寻找UNION关键字:
SELECT ...
UNION [ALL | DISTINCT] SELECT ...
[UNION [ALL | DISTINCT] SELECT ...]
http://dev.mysql.com/doc/refman/5.0/en/union.html
不要忘记清理您的输入,似乎您在查询中放入$keyword
值,而不是之前转义/清理它。您应该查看PDO::prepare
以防止SQL注入(http://www.php.net/manual/fr/pdo.prepare.php)。