[英]Mysql Search in two tables
I have a table name tbl_marketing
contain many column 我有一个表名tbl_marketing
包含很多列
db_id,db_customer,db_brand,db_client.... DB_ID,db_customer,db_brand,db_client ....
Another table name tbl_phonecall
contain many column and linked with tbl_marketing
in db_mid
this contain the id of tbl_marketing
另一个表名tbl_phonecall
包含许多列,并与联tbl_marketing
在db_mid
这个包含的ID tbl_marketing
db_id,db_subject,db_due,db_nextdate,db_mid DB_ID,db_subject,db_due,db_nextdate,db_mid
know i create a search form this search form should do a search on all field on tbl_marketing and on db_due and db_nextdate in tbl_phonecall when the user do his search the result should like this 知道我创建一个搜索表单这个搜索表单应该搜索tbl_marketing上的所有字段以及tbl_phonecall中的db_due和db_nextdate,当用户进行搜索时结果应该是这样的
Customer brand client due nextdate the customer brand and client from tbl_marketing
and the rest from tbl_phonecall
客户品牌客户因nextdate客户对品牌和客户从tbl_marketing
和休息tbl_phonecall
for that search i use this php
code 对于那个搜索我使用这个php
代码
$q = array();
$sql = "";
if(isset($_POST['txt_name']) && !empty($_POST['txt_name'])){
$name = mysqli_real_escape_string($conn,$_POST['txt_name']);
$q[] = "tbl_marketing.db_customer='".$name."' ";
}
if(isset($_POST['txt_client']) && !empty($_POST['txt_client'])){
$client = mysqli_real_escape_string($conn,$_POST['txt_client']);
$q[] = "tbl_marketing.db_client='".$client."' ";
}
if(isset($_POST['txt_brand']) && !empty($_POST['txt_brand'])){
$brand = mysqli_real_escape_string($conn,$_POST['txt_brand']);
$q[] = "tbl_marketing.db_brand='".$brand."' ";
}
if(isset($_POST['txt_dateofcalling']) && !empty($_POST['txt_dateofcalling'])){
$dateofcalling= mysqli_real_escape_string($conn,$_POST['txt_dateofcalling']);
$searchdateofcalling=date("Y-m-d H:i:s", strtotime($dateofcalling));
$q[] = "DATE(tbl_phonecall.db_due)='".$searchdateofcalling."' ";
}
if(isset($_POST['txt_nextdate']) && !empty($_POST['txt_nextdate'])){
$nextdate= mysqli_real_escape_string($conn,$_POST['txt_nextdate']);
$searchnextdate=date("Y-m-d H:i:s", strtotime($nextdate));
$q[] = "DATE(tbl_phonecall.db_nextdate)='".$searchnextdate."' ";
}
$first = true;
foreach($q as $qu){
if($first){
$sql .= " where ".$qu;
$first = false;
}else{
$sql .= " and ".$qu;
}
}
$query=mysqli_query($conn,"select tbl_marketing.*,tbl_phonecall.* from tbl_marketing,tbl_phonecall {$sql}")or die(mysqli_error($conn));
but this query repeat the value 但是这个查询重复了这个值
How can i solve that problem and have a result like i post before 我怎样才能解决这个问题并得到像我之前发布的结果
Looks like you have missed the join condition 看起来你已经错过了加入条件
where tbl_marketing.db_id = tbl_phonecall.db_mid
In case you want to return records from tbl_marketing even if no matching records exist in tbl_phonecall table, use left join in your query 如果您想要从tbl_phoneeting返回记录,即使tbl_phonecall表中不存在匹配的记录,请在查询中使用left join
select tbl_marketing.*,tbl_phonecall.*
from tbl_marketing left join tbl_phonecall
on tbl_marketing.id = tbl_phonecall.mid
where ....<<where conditions here>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.