如何在Mysql查询中组合两个条件?

时间:2014-03-13 07:42:48

标签: php mysql

我的数据库中有三个表。 首先

  

ACCOUNT_TYPE

---------------------------------------------------------------------
|  Sr_No  | Key 1       | Key 2    |   Key 3  |  Key 4   |    Key 5 |
---------------------------------------------------------------------
| 1       |  Person 1   | Person 1 | Person 1 | Person 1 | Person 1 |
| 2       |  Null       |    NULL  | Person 2 | Person 2 | Person 2 | 
| 3       |  Person 2   |    NULL  | Person 3 | NULL     | Null     | 
| 4       |  Null       |    NULL  | NULL     | NULL     | Person 4 | 
| 5       |  Null       |    NULL  | NULL     | Person 5 | Person 5 | 
---------------------------------------------------------------------
  

分支

-----------------------------------------------
|  Sr_No  | Bangalore   | Hyderabad|   Chennai|
-----------------------------------------------
| 1       |  Person 1   | Person 1 | Person 1 |
| 2       |  Null       |    NULL  | Person 2 | 
| 3       |  Person 2   |    NULL  | Person 3 | 
| 4       |  Null       |    NULL  | NULL     | 
| 5       |  Null       |    NULL  | NULL     | 
-----------------------------------------------
  

CUST_CODE

--------------------------------------------------------------
| Cust_code | Name      | Address   |   Branch |   Type      |
--------------------------------------------------------------
| A9999     | Company 1 | Address 1 | Bangalore|  MJA        |
| B672      | Company 2 | Address 2 | Chennai  |  Govt       |
| Z233      | Company 3 | Address 3 | Hyderabad|  KSA        | 
| Q1111     | Company 4 | Address 4 | Bangalore|  Key Account| 
| I665      | Company 1 | Address 5 | Chennai  |  KSA        |
--------------------------------------------------------------

首先是班加罗尔,海德拉巴和钦奈 我有一个要求,即一个人应该只能搜索那些分支应该与分支表中的列匹配的cust_codes,同时只有那些与account_type匹配的客户代码应该是可见的。就像人5一样,帐户类型是key1和key2,他的分支是班加罗尔,而chennai应该只搜索cust_codes,其中分支是bangalore或chennai,类型是key1和key2。

我能够在php中使用if else循环进行分支查询。但同时我也必须过滤掉这些类型。

我尝试过滤掉该位置的代码是

$bangalore="Bangalore";
$hyderabad="Hyderabad";
$chennai="Chennai";
$sess_uname="bmw";
$qry_bangalore=mysql_query("select * from $tbl_name_branch where Bangalore='$sess_uname'");
$count_bangalore=mysql_num_rows($qry_bangalore);

$qry_hyderabad=mysql_query("select * from $tbl_name_branch where Hyderabad='$sess_uname'");
$count_hyderabad=mysql_num_rows($qry_hyderabad);

$qry_chennai=mysql_query("select * from $tbl_name_branch where Chennai='$sess_uname'");
$count_chennai=mysql_num_rows($qry_chennai);

$qry_keyaccount=mysql_query("select * from $tbl_name_acctype where `Key Account`='$sess_uname'");
$count_keyaccount=mysql_num_rows($qry_keyaccount);

$qry_runrate=mysql_query("select * from $tbl_name_acctype where Runrate='$sess_uname'");
$count_runrate=mysql_num_rows($qry_runrate);

$qry_mja=mysql_query("select * from $tbl_name_acctype where MJA='$sess_uname'");
$count_mja=mysql_num_rows($qry_mja);

$qry_govt=mysql_query("select * from $tbl_name_acctype where Govt='$sess_uname'");
$count_govt=mysql_num_rows($qry_govt);

$qry_kso=mysql_query("select * from $tbl_name_acctype where KSO='$sess_uname'");
$count_kso=mysql_num_rows($qry_kso);


if($count_bangalore!=0 && $count_hyderabad!=0 && $count_chennai!=0)
    $qr="select Cust_code,Cust_Name,Cust_City from mgen_custcode where Cust_Name like '%$q%' or Cust_code like '%$q%' order by Cust_Name";
elseif($count_bangalore==0 && $count_hyderabad!=0 && $count_chennai!=0)
    $qr="select Cust_code,Cust_Name,Cust_City from mgen_custcode where Mgen_branch<>'$bangalore' AND (Cust_Name like '%$q%' or Cust_code like '%$q%') order by Cust_Name";
elseif($count_hyderabad==0 && $count_bangalore!=0 && $count_chennai!=0)
    $qr="select Cust_code,Cust_Name,Cust_City from mgen_custcode where Mgen_branch<>'$hyderabad' AND (Cust_Name like '%$q%' or Cust_code like '%$q%') order by Cust_Name";
elseif($count_chennai==0 && $count_bangalore!=0 && $count_hyderabad!=0)
    $qr="select Cust_code,Cust_Name,Cust_City from mgen_custcode where Mgen_branch<>'$chennai' AND (Cust_Name like '%$q%' or Cust_code like '%$q%') order by Cust_Name";
elseif($count_bangalore==0 && $count_hyderabad==0 && $count_chennai!=0)
    $qr="select Cust_code,Cust_Name,Cust_City from mgen_custcode where Mgen_branch=='$chennai' AND (Cust_Name like '%$q%' or Cust_code like '%$q%') order by Cust_Name";
elseif($count_bangalore==0 && $count_hyderabad!=0 && $count_chennai==0)
    $qr="select Cust_code,Cust_Name,Cust_City from mgen_custcode where Mgen_branch=='$hyderabad' AND (Cust_Name like '%$q%' or Cust_code like '%$q%') order by Cust_Name";
elseif($count_bangalore!=0 && $count_hyderabad==0 && $count_chennai==0)
    $qr="select Cust_code,Cust_Name,Cust_City from mgen_custcode where Mgen_branch=='$bangalore' AND (Cust_Name like '%$q%' or Cust_code like '%$q%') order by Cust_Name";
else
    $qr="select Cust_code,Cust_Name,Cust_City from mgen_custcode where Mgen_branch=='America' AND (Cust_Name like '%$q%' or Cust_code like '%$q%') order by Cust_Name";

感谢任何帮助。

0 个答案:

没有答案