查询在HTML文本字段中搜索多个表

时间:2015-04-29 07:34:56

标签: php mysql join

我们有一个HTML搜索页面,有多个要搜索的文本字段。

用户可以在文本字段中输入任意数量的值,在提交时,查询应返回适当的结果。

我们有不同的表格

  • 候选
  • candidate_contact
  • 公司

示例:

用户输入像候选人应该有java技能也住在加利福尼亚州,但不应该有不到2年的经验。

这些记录可以在相同或不同的表中(如同一个表中的技能和国家/地区以及另一个表中的exp)

它包括和排除搜索结果。

$query = array();
if (!empty($_POST['keyword_s_dec'])) 
{
 $query[] = "candidate.cand_desc = '".mysql_real_escape_string($_POST['keyword_s_dec'])."'";
 $join.="JOIN candidate_contact ON candidate.cand_desc=candidate_contact.cand_id"; 
 //$join.="select * from candidate join candidate_contact ON candidate.cand_number=candidate_contact.cand_id WHERE candidate.cand_desc='".$_POST['keyword_s_dec']."'";
 }
 if (!empty($_POT['keyword_s_location'])) 
 {
  $query[] = "candidate_contact.cand_location = '".mysql_real_escape_string($_POST['keyword_s_location;'])."'";///edit
  $join.=" AND JOIN candidate_contact ON candidate.cand_number=candidate_contact.cand_id";
 } 

//$condition = implode(' AND ', $query);
$condition = implode(' AND ', $query);
$sql = "SELECT * FROM candidate".$join.' where '.$condition;

where - candidate is my main table and candidate_contact is another table.
where -cand_desc is database column in candidate & keyword_s-dec is text field id.
where- cand_location is my database column in candidate_contact and keywors_s_location is text field id.

This code is being slightly guided by one of stack overflow member and we though this should be edit to get more precised to the problem.

2 个答案:

答案 0 :(得分:0)

SELECT candidate.*,candidate_contact.feild_name1,candidate_contact.feild_name2
FROM candidate 
LEFT JOIN candidate_contact  
ON candidate.cand_number=candidate_contact.cand_id where $condition

注意: - 在where子句中添加带有。(点)的表名,以定义要进行比较的字段名称,如查询的选择字段区域所示,并且可以继续以与以下相同的方式连接不同的表。在where子句之前。

希望这能解决您的问题

答案 1 :(得分:0)

if (!empty($_POST['keyword_s_dec'])) 
{
$query[] = "candidate.cand_desc = '".mysql_real_escape_string($_POST['keyword_s_dec'])."'";
$join.=" JOIN candidate_contact ON candidate.cand_number=candidate_contact.cand_id"; 
}
if (!empty($_POST['keyword_s_preflocation'])) 
{
$query[] = "candidate_contact.cand_location = '".mysql_real_escape_string($_POST['keyword_s_preflocation'])."'";///edit
//$join.=" LEFT JOIN candidate_contact ON candidate.cand_number=candidate_contact.cand_id";
}

$condition = implode(' AND ', $query);
$sql = "SELECT * FROM candidate ".$join.' where '.$condition;

@ karvin.developer如果连接条件相同,则无效。我犯了一些错误,但从一开始就重新阅读你的陈述,这对我来说就像天堂一样。谢谢karvin.developer:)