MySQL change view column from BIGINT to BIT

I have a table like this.

(BIGINT)     (BIGINT)
PLAYER_ID    FRIEND_ID
----------------------
1            2
2            1
1            3

I think I can make a View as

SELECT * FROM FRIEND F1 LEFT OUTER JOIN FRIEND F2 ON F1.FRIEND_ID = F2.PLAYER_ID

printing

F1.PLAYER_ID    F1.FRIEND_ID    F2.PLAYER_ID    F2.FRIEND_ID
[BIGINT]        [BIGINT]        [BIGINT]        [BIGINT]
------------------------------------------------------------
1               2               2               1
2               1               1               2
1               3               NULL            NULL

Can I make a View which represents those F2's existence AS BIT?

F1.PLAYER_ID    F1.FRIEND_ID    BACK
[BIGINT]        [BIGINT]        [BIT]
-------------------------------------
1               2               1     // exist (1 -> 2)
2               1               1     // exist (2 -> 1)
1               3               0     // no backing relation (3 -> 1)

UPDATE ------------------------------------------------------------------

Sorry the above query wan't perfect.

The query, with @Romil 's answer, must be written like this.

SELECT
    F1.PLAYER_ID,
    F1.FRIEND_ID,
    CASE WHEN F2.FRIEND_ID IS NULL THEN 0 ELSE 1 END AS BACK
FROM FRIEND F1
    LEFT OUTER JOIN FRIEND F2
        ON F1.FRIEND_ID = F2.PLAYER_ID AND F1.PLAYER_ID = F2.FRIEND_ID

Answers


SELECT F1.PLAYER_ID, 
       F2.PLAYER_ID, 
       CASE 
         WHEN F2.PLAYER_ID IS NULL THEN 0 
         ELSE 1 
       end AS Back 
FROM   FRIEND F1 
       LEFT OUTER JOIN FRIEND F2 
                    ON F1.FRIEND_ID = F2.PLAYER_ID 

SELECT *, EXISTS (SELECT * FROM FRIEND WHERE PLAYER_ID = F1.FRIEND_ID) AS BACK
FROM   FRIEND AS F1

See it on sqlfiddle.


You could try something along the lines of

SELECT F1.*, if(F1.FRIEND_ID = F2.PLAYER_ID,1,0) FROM FRIEND F1 LEFT JOIN FRIEND F2 ON F1.FRIEND_ID = F2.PLAYER_ID

Need Your Help

How to make this as Array?

php javascript arrays

I am trying to put my data inside Highchart Library. There it is showing sample as following code.