Mysql请求 - 获取联系人

时间:2016-04-08 21:34:39

标签: mysql request

我有联系人表格

from_user_id   to_user_id
300            301
301            298

我正在尝试查找user_id = 301的所有唯一联系人。对于此用户,它将是:300和298;

我已尝试过此请求,但结果错误:

SELECT 
    u.name,
    IF(c.from_user_id = '.(int)$user->id.', c.from_user_id, c.to_user_id) AS uid
FROM 
    `#__contacts`   AS c
LEFT JOIN 
    `#__users`  
AS 
    u   
ON
    uid = u.id

我该怎么做? 谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT u.name
FROM `#__contacts` AS c
LEFT JOIN `#__users` AS u ON c.to_user_id = u.id
WHERE '.(int)$user->id.' IN (c.from_user_id, c.to_user_id)
ORDER BY c.created DESC