我正在使用(并加入)三个表进行查询,但我有一个错误,我无法理解我正在做的查询。我在查询语句中使用IF,AS和IN。这是查询:
SELECT DISTINCT users.id, users.firstname, users.lastname, friends.recipient, friends.sender, message.sender, message.recipient, message.date, message.time
FROM users
JOIN friends
ON users.id IN (friends.sender, friends.recipient)
JOIN message
ON (users.id = message.recipient OR users.id = message.sender)
WHERE 75 IN (friends.sender,friends.recipient)
OR friends.status = 1 IF(message.recipient = 75, 'message.recieved', 'message.sent') AS message.direction
FROM message WHERE message.recipient IN (75, 129) AND sender IN (75, 129)
这是导致错误的行:
IF(message.recipient = 75,' message.recieved',' message.sent')AS message.direction
答案 0 :(得分:1)
您似乎分别编写了两个查询:
SELECT DISTINCT users.id, users.firstname, users.lastname,
friends.recipient, friends.sender, message.sender,
message.recipient, message.date, message.time
FROM users
JOIN friends
ON users.id IN (friends.sender, friends.recipient)
JOIN message
ON (users.id = message.recipient OR users.id = message.sender)
WHERE 75 IN (friends.sender,friends.recipient)
OR friends.status = 1
SELECT IF(message.recipient = 75, 'message.recieved',
'message.sent') AS message.direction FROM message WHERE
message.recipient IN (75, 129) AND sender IN (75, 129)
一起做:
SELECT DISTINCT users.id, users.firstname, users.lastname, friends.recipient, friends.sender, message.sender, message.recipient, message.date, message.time , IF(message.recipient = 75, 'message.recieved', 'message.sent') AS message.direction
FROM users
JOIN friends
ON users.id IN (friends.sender, friends.recipient)
JOIN message
ON (users.id = message.recipient OR users.id = message.sender)
WHERE ( 75 IN (friends.sender,friends.recipient)
OR friends.status = 1 )
AND message.recipient IN (75, 129)
AND message.sender IN (75, 129)