2014-03-31 102 views
0

我在mysql數據庫中使用LIKE關鍵字過濾數據。這是下面的查詢:使用Mysql中的where和Like子句過濾數據

Select c.c_id, c.c_name, slab, 
COUNT(c.c_id) as tot_visit, 
SUM(t.bill) as tot_revenue, c.priority, c.ratings 
From tbl_customer c 
inner join tbl_transaction t on c.c_id=t.c_id 
group by c.c_id 
Where r_id="r1" AND c.slab LIKE "%teen%" 

當我刪除此 - > LIKE「%青少年%」 < 和c.occupation - 子句,查詢工作fine..otherwise它給空集行。

我在努力但沒有克服它。任何幫助,將不勝感激。

編輯 我正在篩選青少年,年輕人和年長階層的數據。相同的代碼在其他php文件中工作正常,但不在此。

if(isset($_GET['submit'])) 
{ 
    if (isset($_GET["teen"])) 
    { 
     $arguments[] = "c.slab LIKE '%teen%'"; 
    } 
    if (isset($_GET["young"])) { 
     $arguments[] = "c.slab LIKE '%young%'"; 
    } 
    if (isset($_GET["senior"])) 
    { 
     $arguments[] = "c.slab LIKE '%senior%'"; 
    } 

    if(!empty($arguments)) { 
     $str = implode(' or ',$arguments); 
     $qry = mysql_query('Select c.c_id, c.c_name, c.slab, 
COUNT(c.c_id) as tot_visit, 
SUM(t.bill) as tot_revenue, c.priority, c.ratings 
From tbl_customer c 
inner join tbl_transaction t on c.c_id=t.c_id 
Where r_id="r1" AND "'.$str.'" 
group by c.c_id'); 

沒有得到這一切的錯誤。請幫助。

$qry = mysql_query('Select c.c_id, c.c_name, c.slab, 
COUNT(c.c_id) as tot_visit, 
SUM(t.bill) as tot_revenue, c.priority, c.ratings 
From tbl_customer c 
inner join tbl_transaction t on c.c_id=t.c_id 
Where r_id="r1" AND "'.$str.'" 
group by c.c_id'); 

這條線是ambigous:

Where r_id="r1" AND "'.$str.'" 

由於變量$海峽會是什麼樣

+1

將分句之前的where子句放在分組條件之前 – Sadikhasan

+1

您的條件不滿足任何條件,這就是爲什麼返回空結果集 – Sadikhasan

+0

'Where r_id =「r1」''''r_id'可能應該是't.r_id'?在選擇'slab'也沒有前綴 – DanFromGermany

回答

2

嘗試之前groupby

Select c.c_id, c.c_name, slab, 
COUNT(c.c_id) as tot_visit, 
SUM(t.bill) as tot_revenue, c.priority, c.ratings 
From tbl_customer c 
inner join tbl_transaction t on c.c_id=t.c_id 
Where r_id="r1" AND c.occupation LIKE "%teen%" 
group by c.c_id 
+1

完成它..錯誤不會發生,但導致空行.. – Divyang

+1

大概沒有行匹配您的條件。 –

0

在下面的查詢寫where條件這個:

Where r_id="r1" AND '.$str.' 

感謝Stackoverflow的所有幫助。