从两列sql中找到相同的值

时间:2017-01-02 10:53:05

标签: mysql sql

我想找到id跟随并从一个表中跟回用户...

    

在这里,我想只获取那些像这里一样追随的数据

1是跟随5和5也跟着回到1所以当我触发查询时,只看到cid = 1和u_id = 5



<pre class="lang-sql prettyprint prettyprinted"><code><span class="kwd">cid</span><span class="pln">              </span><span class="kwd">u_id</span><span class="pln"> 
</span><span class="com">---------            ---------</span><span class="pln">
</span><span class="lit">1</span><span class="pln">                5
</span><span class="lit">2</span><span class="pln">                23
</span><span class="lit">3</span><span class="pln">                </span><span class="lit">45</span><span class="pln">
</span><span class="lit">4</span><span class="pln">                </span><span class="lit">87</span><span class="pln">
</span><span class="lit">5</span><span class="pln">                1</span></code></pre>
&#13;
&#13;
&#13;

这是查询

SELECT * FROM `recent_activities` WHERE cid=1 

我想从cid中找到它..请建议

1 个答案:

答案 0 :(得分:0)

试试这个,(在这里使用自我加入来解决问题)。

SELECT  T1.cid,T1.u_id 
FROM    TABLE1 AS T1
    INNER JOIN TABLE1 AS T2 ON T1.cid = T2.u_id AND T1.u_id = T2.cid