my sql table is something like this:
+----+------------+--------+---------------+---------------+-------------+----------+-------------+---------------------+
| id | msisdn | status | reason | CallRejection | Unavailable | NoAnswer | NetworkBusy | autotimestamp |
+----+------------+--------+---------------+---------------+-------------+----------+-------------+---------------------+
| 1 | 9999999901 | Failed | NoAnswer | 1 | 2 | 2 | 4 | 2013-12-19 15:46:50 |
| 2 | 9999999902 | Failed | NetworkBusy | 4 | 2 | 3 | 2 | 2013-12-19 13:07:42 |
| 3 | 9999999903 | Failed | CallRejection | 6 | 6 | 6 | 5 | 2013-12-19 13:07:53 |
| 4 | 9999999904 | Failed | Unavailable | 2 | 4 | 2 | 0 | 2013-12-19 13:08:03 |
| 5 | 9999999905 | Failed | Misc | 6 | 2 | 6 | 2 | 2013-12-19 13:07:01 |
| 6 | 9999999906 | Failed | NoAnswer | 1 | 3 | 1 | 6 | 2013-12-19 13:07:24 |
| 7 | 9999999907 | Failed | NetworkBusy | 6 | 6 | 2 | 3 | 2013-12-19 13:07:42 |
| 8 | 9999999908 | Failed | CallRejection | 8 | 3 | 3 | 0 | 2013-12-19 13:07:53 |
| 9 | 9999999909 | Failed | Unavailable | 3 | 8 | 5 | 6 | 2013-12-19 13:08:03 |
| 10 | 9999999910 | Failed | Misc | 8 | 4 | 0 | 4 | 2013-12-19 13:07:01 |
+----+------------+--------+---------------+---------------+-------------+----------+-------------+---------------------+
I want to extract the value of each reason based on the value of reason
column for each row. eg:
row 1
should give 2
as a result because the reason
column has noanswer
, so the value of that particular column ie noanswer
should be returned.
Actually, I have to select multiple rows after comparing the returned valued with a constant. I'm able to do this by the following method :
select @col := reason from msisdn_table where id=1;
SET @a = concat('select ', @col, ' from msisdn_table where id=1');
PREPARE STMT FROM @a;
EXECUTE STMT;
DEALLOCATE PREPARE STMT;
But this is not very promising solution because it is for only one row and i'll have to use a for loop in order to get chunk's of 1000 rows, and that would not be very efficient. What is the more efficient way?
Try this:
SELECT id,
reason
CASE
WHEN reason = 'NoAnswer' THEN NoAnswer
WHEN reason = 'NetworkBusy' THEN NetworkBusy
END AS value
FROM table1
It should give you an output like this:
+----+------------+----------+
| id | reason | value |
+----+------------+----------+
| 1 | NoAnswer | 2 |
+----+------------+----------+
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.