2016-02-04 73 views
1

我想從單個表中獲取值行。我想爲特定的id獲取sub_id。從同一表(hirarical)獲取多個值

我在2查詢中實現了我的要求。我想在單個查詢中完成它。我想要顯示結果'事件,訂單歷史記錄,活動票據,calander'。

enter image description here

$sql="select * from table1 where roles like %admin% and sub_id='0'" 
$sql1=mysql_query($sql); 
while($fet=mysql_fetch_assoc($sql1)) 
{ 
    $id=$fet['id']; 
    $query="select page_name from table1 where sub_id= '$id'"; 
    .. .. 
} 
+0

如果你已經有了具體的ID爲什麼不直接查詢的子類別? –

+0

請解釋你的問題。 –

回答

1

,你需要的是subqueries

基本上,您希望選擇一些匹配由另一個查詢結果給出的條件的行。

下面的例子都應該解決您的問題,並澄清這一概念:

select 
    page_name 
from 
    table1 
where 
    sub_id in (
    select 
     id 
    from 
     table1 
    where 
     roles like '%admin%' 
     and sub_id = 0 
) 
+0

儘管這可能可以回答這個問題,但最好提供一些關於此代碼如何提供幫助的解釋。 – vard