2012-05-28 114 views
0

在我的查詢中,我想檢查具有指定權限的用戶是否存在。目前我正在使用下面的查詢。它將檢索具有指定條件的授權用戶行。格式化mysql查詢輸出

select a.ixUserAuthorization from tblechecklistuserroleassignmentxref r 
inner join tblechecklistuserrole u on u.ixUserRole=r.ixUserRole and u.sname='Initiator' 
inner join tblechecklistuserauthorization a on a.ixUserAuthorization=r.ixUserAuthorization 
and a.ixcustomer='1'and a.ixprogram='1'and a.ixworkpackage='1'and a.ixactivity='1' and a.ixUser='626e28e8-e67a-4d11-8d2c-129d0ab79e96'; 

如果任何行返回我想要顯示的結果爲真,如果沒有行從上述查詢我想要顯示的結果爲假返回。

如何修改以獲得結果爲true或false。

+0

?如何計算選擇查詢結果的行數? –

+1

在結果上使用函數mysql_num_rows() – Uttara

回答

1
SELECT CASE 
     WHEN Count(*) >= 1 THEN 'true' 
     ELSE 'false' 
     END AS Answer 
FROM (SELECT a.ixUserAuthorization 
     FROM tblechecklistuserroleassignmentxref r 
       INNER JOIN tblechecklistuserrole u 
       ON u.ixUserRole = r.ixUserRole 
        AND u.sname = 'Initiator' 
       INNER JOIN tblechecklistuserauthorization a 
       ON a.ixUserAuthorization = r.ixUserAuthorization 
        AND a.ixcustomer = '1' 
        AND a.ixprogram = '1' 
        AND a.ixworkpackage = '1' 
        AND a.ixactivity = '1' 
        AND a.ixUser = '626e28e8-e67a-4d11-8d2c-129d0ab79e96') a 
+0

我得到了所需的結果..僅供參考你能解釋爲什麼'a'最後需要。 –

+1

每個內部Sql都需要提供一個別名,使其成爲外部From語句的一部分。 –

1

使用功能mysql_num_rows

例如:

if(mysql_num_rows($result)) 
    echo "true"; 
else 
    echo "false"; 

其中$結果是查詢的結果是否計數()可以用來

+1

你怎麼知道php是否被使用? – Pablo

+1

以及我的錯誤 – Uttara