0

正しい結果を返さないクエリがあります:

 SELECT t.GroupName AS GroupName, t.ApplicationName AS ApplicationName, t.UserName
        FROM UserApplication t
        WHERE (@ApplicationName IS NULL OR @ApplicationName = '' OR t.ApplicationName = @ApplicationName) AND
              (@UserName IS NULL OR @UserName = '' OR t.UserName= @UserName );

テーブル構造:

CREATE TABLE userapplication
            (`ID` INT,
             `ApplicationName` VARCHAR(100),
             `GroupName` VARCHAR(100),
             `UserName` VARCHAR(100))

パラメータに値を渡さないと、テーブルのすべての行が表示されますが、パラメータ @ApplicationName または @UserName に値を渡すと、同じ結果が得られます。助けてください

4

1 に答える 1

1

If you correctly set values of user variables your query will work just fine

SET @ApplicationName = 'App 1';
SET @UserName = '';

SELECT t.GroupName, 
       t.ApplicationName, 
       t.UserName
  FROM UserApplication t
 WHERE (COALESCE(@ApplicationName, '') = '' 
     OR t.ApplicationName = @ApplicationName) 
   AND (COALESCE(@UserName, '') = '' 
     OR t.UserName= @UserName);

It's a little bit more succinct version of your query

Here is SQLFiddle demo

于 2013-06-12T06:08:04.557 に答える