有条件地选择MySQL

时间:2015-08-07 08:40:11

标签: mysql

我有一个特殊情况,我需要获取用户名和用户图像,即"对面"给定的user_id。我的意思是,如果您的用户ID为10,并且您希望获得与该用户相关的消息,那么我希望用户名和用户图像与user_id为10的用户相反。所以如果有人如果user_id为3已经向user_id为10的某个人发送了一条消息,那么我希望选择该消息,但我只想要没有user_id 10的用户的用户名和用户图像,这是我给出的user_id

我有3个表可供使用:

用户表:

+--------------+------------------+------+-----+---------+----------------+
| Field        | Type             | Null | Key | Default | Extra          |
+--------------+------------------+------+-----+---------+----------------+
| id           | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| email        | varchar(255)     | NO   | MUL | NULL    |                |
| username     | varchar(40)      | NO   | MUL | NULL    |                |
| geonameid    | int(11) unsigned | NO   |     | NULL    |                |
| birthdate    | date             | NO   |     | NULL    |                |
| hash         | varchar(255)     | NO   |     | NULL    |                |
| image_id     | int(11) unsigned | YES  |     | NULL    |                |
| about        | text             | YES  |     | NULL    |                |
| verified     | int(1)           | NO   |     | 0       |                |
| notification | int(1)           | NO   |     | 1       |                |
| joined       | int(11) unsigned | NO   |     | NULL    |                |
| lastactivity | int(11) unsigned | NO   |     | NULL    |                |
| ipv4         | int(11) unsigned | YES  |     | NULL    |                |
| ipv6         | binary(16)       | YES  |     | NULL    |                |
| deleted      | int(1)           | NO   |     | 0       |                |
+--------------+------------------+------+-----+---------+----------------+

图像表:

+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| id        | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| name      | varchar(255)     | NO   |     | NULL    |                |
| width     | int(5) unsigned  | NO   |     | NULL    |                |
| height    | int(5) unsigned  | NO   |     | NULL    |                |
| extension | varchar(10)      | NO   |     | NULL    |                |
| created   | int(11) unsigned | NO   |     | NULL    |                |
| user_id   | int(11) unsigned | NO   | MUL | NULL    |                |
| deleted   | int(1)           | NO   |     | 0       |                |
+-----------+------------------+------+-----+---------+----------------+

和消息表:

+--------------+------------------+------+-----+---------+----------------+
| Field        | Type             | Null | Key | Default | Extra          |
+--------------+------------------+------+-----+---------+----------------+
| id           | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| message      | text             | NO   |     | NULL    |                |
| reply_to     | int(11) unsigned | NO   | MUL | 0       |                |
| from_id      | int(11) unsigned | NO   | MUL | NULL    |                |
| to_id        | int(11) unsigned | NO   | MUL | NULL    |                |
| active       | int(1)           | NO   |     | 1       |                |
| sent         | int(11) unsigned | NO   | MUL | NULL    |                |
| viewed       | int(1)           | NO   |     | 0       |                |
| from_deleted | int(1)           | NO   |     | 0       |                |
| to_deleted   | int(1)           | NO   |     | 0       |                |
| deleted      | int(1)           | NO   |     | 0       |                |
+--------------+------------------+------+-----+---------+----------------+

现在我有这个问题:

    SELECT
        message.id,
        message.message,
        message.reply_to AS replyTo,
        message.from_id AS fromId,
        message.to_id AS toId,
        message.sent,
        message.viewed,
        fromuser.username as fromUsername,
        touser.username as toUsername,
        CONCAT('".UPLOAD_URL."/img/', SUBSTR(image.name, 1, 2), '/', SUBSTR(image.name, 3, 2), '/', SUBSTR(image.name, 5, 2), '/', image.name, '-50x50', '.' ,image.extension) AS thumbnail50x50
    FROM
        message AS message
    INNER JOIN user AS fromuser
        ON message.from_id = fromuser.id
    INNER JOIN user AS touser
        ON message.to_id = touser.id
    INNER JOIN image AS image
        ON image.id = fromuser.image_id
    WHERE
        (message.from_id = :user_id OR message.to_id = :user_id) AND
        message.active = 1 AND
        message.deleted = 0
    ORDER BY message.sent DESC;

而不是选择fromuser.username并选择touser.username 并选择已发送消息的用户的图像, 我想选择像:oppositeuser.username和相反用户的image.name,如上面的查询中所做的那样。这里的特殊情况是我需要给定user_id的对方用户的用户名和用户图像,而不是fromuser和touser。

1 个答案:

答案 0 :(得分:0)

尝试使用IF()。

SELECT
    message.id,
    message.message,
    message.reply_to AS replyTo,
    message.from_id AS fromId,
    message.to_id AS toId,
    message.sent,
    message.viewed,
    IF(message.from_id = :user_id, touser.username, fromuser.username) as oppositeName,
    IF(message.from_id = :user_id, 
        CONCAT('".UPLOAD_URL."/img/', SUBSTR(toimage.name, 1, 2), '/', SUBSTR(toimage.name, 3, 2), '/', SUBSTR(toimage.name, 5, 2), '/', toimage.name, '-50x50', '.' ,toimage.extension), 
        CONCAT('".UPLOAD_URL."/img/', SUBSTR(fromimage.name, 1, 2), '/', SUBSTR(fromimage.name, 3, 2), '/', SUBSTR(fromimage.name, 5, 2), '/', fromimage.name, '-50x50', '.' ,fromimage.extension) 
        ) AS thumbnail50x50
FROM
    message AS message
INNER JOIN user AS fromuser
    ON message.from_id = fromuser.id
INNER JOIN user AS touser
    ON message.to_id = touser.id
INNER JOIN image AS fromimage
    ON fromimage.id = fromuser.image_id
INNER JOIN image AS toimage
    ON toimage.id = touser.image_id
WHERE
    (message.from_id = :user_id OR message.to_id = :user_id) AND
    message.active = 1 AND
    message.deleted = 0
ORDER BY message.sent DESC;