[英]selecting a value from multiple tables
我已將搜索選項添加到項目中。 當某些特定用戶在搜索文本框中輸入內容時,我使用了jQuery自動完成功能來顯示關鍵字的相關結果。
這是我的PHP腳本。
<?php
require_once('database.php');
if(isset($_POST['queryString'])) {
$queryString = $dbc->real_escape_string($_POST['queryString']);
if(strlen($queryString) >0) {
//$query = $dbc->query("SELECT subjects FROM subject WHERE subjects LIKE '$queryString%' LIMIT 10");
$q = "SELECT tutor_name FROM tutors WHERE tutor_name LIKE '%$queryString%' LIMIT 10";
$r = mysqli_query ( $dbc, $q);
if($q) {
while ($row = mysqli_fetch_array($r, MYSQL_ASSOC)) {
echo '<li onClick="fill(\''.$row['tutor_name'].'\');">'.$row['tutor_name'].'</li>';
}
} else {
echo 'ERROR: There was a problem with the query.';
}
} else {
}
} else {
echo 'There should be no direct access to this script!';
}
?>
這對我來說是正常工作。.但是我的問題是我需要使用輸入關鍵字檢查多個表。 在這里,我僅使用一個表..任何人都可以告訴我如何將其他表附加到該查詢?
使用UNION
SELECT keyword
FROM
(
SELECT tutor_Name AS keyword FROM tutors
UNION
SELECT subject_name AS keyword FROM subjects
UNION
SELECT institute_name AS keyword FROM institutes
) s
WHERE keyword LIKE '%$queryString%'
LIMIT 10
您查詢的SQL Injection
也容易受到攻擊,請閱讀下面的文章
下面的Query merge three tables
,這些Query merge three tables
僅basis of common column in the tables
selective records present in tables
basis of common column in the tables
returns
只selective records present in tables
那些selective records present in tables
。
SELECT tutor_name FROM yourDB
LEFT JOIN tableA ON tutor_name = '%$queryString%'
LEFT JOIN tableB ON tutor_name = '%$queryString%';
希望這對您有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.