如何从带有子查询的两个表中获取值?

时间:2015-04-23 08:13:00

标签: php mysql sql-server

我有两个表名是feed和友情....

友谊表有这些字段.toid是会话用户id,fromid是朋友id,状态可能是0或1,feed表包含id,post_user_id是feed海报id,内容。现在我想从我的朋友那里得到饲料表中的饲料我可以从我的朋友那里获得饲料,但我无法获得自己的饲料,当我在查询执行的条件下放入自己的身份证但是结果为空。我试过这些

    SELECT * FROM feed WHERE `post_user_id` IN (Select fromid from 
     gr_user_friendships where toid = 47) ORDER BY 
      post_date_time DESC 

/ ****这个查询只给我的朋友记录以获取我的记录,我也将自己的身份证放在条件中**** /

   SELECT * FROM feed WHERE `post_user_id` IN (Select fromid from 
     gr_user_friendships where toid = 47) and `post_user_id` = 47 ORDER
      BY post_date_time DESC 

/ *这次结果为空** /

我也试过这个内部联接没有成功

4 个答案:

答案 0 :(得分:4)

不应该使用OR而不是AND吗?

SELECT * FROM feed WHERE `post_user_id` IN (Select fromid from 
 gr_user_friendships where toid = 47) OR `post_user_id` = 47 ORDER
  BY post_date_time DESC

答案 1 :(得分:2)

在第二个查询中:

SELECT * FROM feed WHERE `post_user_id` IN (Select fromid from 
     gr_user_friendships where toid = 47) and `post_user_id` = 47 ORDER
      BY post_date_time DESC

只需将and替换为or

答案 2 :(得分:2)

尝试:

SELECT * FROM feed WHERE post_user_id = 47 or 'post_user_id' IN 
(Select fromid from gr_user_friendships where toid = 47) ORDER BY 
 post_date_time DESC 

答案 3 :(得分:0)

试试这个..

SELECT * FROM feed INNER JOIN (Select fromid from 
 gr_user_friendships where toid = 47) WHERE 'toid' =  47 and `post_user_id` =    47 ORDER
  BY post_date_time DESC