1
mysql> SELECT
   -> IF(status='EndSP',reportId,'') as 'reportId_EndSP'
   -> FROM T_Name LIMIT 10;
+--------------+
| reportId_EndSP |
+--------------+
|                |
|                |
| 270241         |
|                |
| 270242         |
|                |
|                |
| 270244         |
|                |
|                |
+--------------+
10 rows in set (0.00 sec)

But i need to return only the row where status = 'EndSP' 

else 句を実行する必要はありません。

私は単に書くことでそれを達成することができます

SELECT reportId FROM T_Name where status='EndSP';

しかし、ifまたはcaseを使用してそれを行う必要があります。

編集/更新

私の実際のクエリは次のようになります

SELECT
   -> IF(status='EndSP',reportId,'') as 'reportId_EndSP',
   -> IF(status='EndSP',createdTS,'') as 'createdTS_EndSP',
   -> IF(status='BeginSP',reportId,'') as 'reportId_BeginSP',
   -> IF(status='BeginSP',createdTS,'') as 'createdTS_BeginSP'
   -> FROM T_Name HAVING reportId_EndSP!='' AND reportId_BeginSP!='' LIMIT  10;
4

3 に答える 3

2

どうですか:

SELECT * FROM (
   SELECT
      IF(status='EndSP',reportId,'') as reportId_EndSP
      FROM T_Name) a
WHERE reportId_EndSP != '' LIMIT 10;

デモhttp://sqlfiddle.com/#!2/d1167/8

または単一選択バージョン

SELECT
   IF(status='EndSP',reportId,'') as reportId_EndSP
   FROM T_Name
HAVING reportId_EndSP != '' LIMIT 10;

デモhttp://sqlfiddle.com/#!2/d1167/9

于 2012-10-09T10:11:30.750 に答える
1

/のみ を使用することはできません。これは、結果セットから値を除外しないためです。またはが呼び出されたときに、結果セットはすでに決定されています。IFCASEIFCASE

于 2012-10-09T10:01:02.350 に答える
0
SELECT
 reportId as 'reportId_EndSP'
 FROM T_Name where  IF(status='EndSP',reportId,' ') LIMIT 10;
于 2012-10-09T10:08:08.473 に答える