1
SELECT *
FROM `history`
WHERE `site_id` = '1'
    AND `user_id` = '1'
ORDER BY `date` DESC 
LIMIT 0 , 20

history表中是字段child_user_id。我需要检查child_users表中是否存在此 id,如果存在 - 将其替换为child_user_namefrom child_users,否则user_nameusers表中设置 whereuser_id = '1'

4

2 回答 2

1

尝试这个:

SET @useid =1;

SELECT case when c.id IS null then @useid 
       else (select user_name from  users where user_id=@useid) end 
FROM   history  h 
LEFT OUTER JOIN child_users c
ON     h.child_user_id=c.id
于 2012-09-17T06:33:29.887 回答
1

尝试这个

     SELECT * FROM `history`  LEFT OUTER JOIN  child_users 
      ON     history.child_user_id=child_user_id  WHERE `site_id` = '1'AND `user_id` = '1'
于 2012-09-17T06:58:04.993 回答