0

I have a database with this table(rating)..

+---------+-------------+----------+------+------------+
| rate_id | reviewer_id | movie_id | rate | date1      |
+---------+-------------+----------+------+------------+
|       1 |         201 |      101 | 2    | 2011-01-22 |
|       2 |         201 |      101 | 4    | 2011-01-27 |
|       3 |         202 |      106 | 4    | NULL       |
|       4 |         203 |      103 | 2    | 2011-01-20 |
|       5 |         203 |      108 | 4    | 2011-01-12 |
|       6 |         203 |      108 | 2    | 2011-01-30 |
|       7 |         204 |      101 | 3    | 2011-01-09 |
|       8 |         205 |      103 | 3    | 2011-01-27 |
|       9 |         205 |      104 | 2    | 2011-01-22 |
|      10 |         205 |      108 | 4    | NULL       |
|      11 |         206 |      107 | 3    | 2011-01-15 |
|      12 |         206 |      106 | 5    | 2011-01-19 |
|      13 |         207 |      107 | 5    | 2011-01-20 |
|      14 |         208 |      104 | 3    | 2011-01-02 |
+---------+-------------+----------+------+------------+

the rate is a set which has those values ('1','2','3','4','5'). i want to convert this set values to integers. i used cast(rate as unsigned) .then i used this query.. SELECT rate,cast(rate AS UNSIGNED) FROM rating;

it gives me this table

mysql> SELECT rate,cast(rate as unsigned) FROM rating;

+------+------------------------+
| rate | cast(rate as unsigned) |
+------+------------------------+
| 2    |                      2 |
| 4    |                      8 |
| 4    |                      8 |
| 2    |                      2 |
| 4    |                      8 |
| 2    |                      2 |
| 3    |                      4 |
| 3    |                      4 |
| 2    |                      2 |
| 4    |                      8 |
| 3    |                      4 |
| 5    |                     16 |
| 5    |                     16 |
| 3    |                      4 |
+------+------------------------+

it looks like binary values..plz help me to convert this into correct integer values..

3 Answers 3

0

Try this..

CAST (CAST(rate AS char) AS UNSIGNED);
it works..

0

ENUM is not a good selection for number values. instead of using ENUM you can use TYNYINT and filter values for only accept [1-5] in your code.

MYSQL document mention that clearly

We strongly recommend that you do not use numbers as enumeration values, because it does not save on storage over the appropriate TINYINT or SMALLINT type, and it is easy to mix up the strings and the underlying number values (which might not be the same) if you quote the ENUM values incorrectly. If you do use a number as an enumeration value, always enclose it in quotation marks. If the quotation marks are omitted, the number is regarded as an index. See Handling of Enumeration Literals to see how even a quoted number could be mistakenly used as a numeric index value.

Check this out : MYSQL DOC Enumeration Limitations

:)

0

You should try with this following ways.

The type for the result can be one of the following values:

  • BINARY[(N)]
  • CHAR[(N)]
  • DATE
  • DATETIME
  • DECIMAL[(M[,D])]
  • SIGNED [INTEGER]
  • TIME
  • UNSIGNED [INTEGER]

Thanks.

1
  • thanks your answer gives me a clue to find the answer..:-)
    – user4769997
    Aug 17, 2015 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.