mysql 左串連 自串連 例子

來源:互聯網
上載者:User

串連就是將兩個表按照某個公用欄位來拼成一個大表。

左串連就是在做串連是以左邊這個表為標準,來遍曆右邊的表。


1、引子

左串連,自串連


SELECT * FROM test_visit t1 LEFT JOIN test_visit t2 ON t1.uid = t2.uid

結果:




2、問題 例子:

使用者訪問記錄:


問題:查出看了湖南衛視但沒有看北京衛視的使用者資訊

邏輯:先通過左串連將看了湖南衛視和北京衛視的查出來,然後再將看了湖南衛視但不在剛才查出的結果中的使用者查出來。

SELECT * FROM test_visit WHERE channel='湖南衛視' AND uid NOT IN (SELECT DISTINCT t1.uid FROM test_visit t1 LEFT JOIN test_visit t2 ON t1.uid = t2.uidWHERE t1.channel='湖南衛視' AND t2.channel='北京衛視' )


結果:


各位大神,有什麼好的方法,說說吧,我感覺這個不是最優的

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.