mysql 聯合查詢 join 用法舉例

2021-06-29 11:25:48 字數 859 閱讀 3626

■最好在相同字段進行比較操作,在建立好的索引欄位上儘量減少函式操作(1).選取最適用的字段屬性,應該盡量把字段設定為not null,這樣在將來執行查詢的時候,資料庫不用去比較null值。

(2).使用連線(join)來代替子查詢(sub-queries)

(3).盡量少使用 like 關鍵字和萬用字元

列出所用文章與使用者一一對應的資料。

select ... inner join ... on 語句如下:

select article.aid,article.title,user.username from article inner join user on article.uid = user.uid

等同與下面的 sql 語句:

select article.aid,article.title,user.username from article,user where article.uid = user.uid

left join 會取得左表(table1)全部記錄,即使右表(table2)並無對應匹配記錄。left join 基本語法如下:

... from table1 left join table2 on condition ...

當使用 union 時,mysql 會把結果集中重複的記錄刪掉,而使用 union all ,mysql 會把所有的記錄返回,且效率高於 union。

select aid,title from article union select bid,title from blog

乙個子查詢的例子如下:

select * from article where uid in(select uid from user where status=1)

sql語句的聯合查詢 join 用法

student no,name,classid,1,zdy,2,2,huz,2,3,yxx,2,4,sss,1,class classid,classname,2,y2002,3,y2003,1 select from student class where student.classid clas...

mysql聯合查詢

有乙個前提很重要 就是兩個表中的對應字段應該是建立聯合關係且該鍵應唯一 在查詢該聯合建的時候要指明 表.欄位 1.select from 表a,表a子表 where表a.filecode 表a子表.filecodeand表a.id in select 表a子表 id from 表a子表 where ...

MySQL聯合查詢

1.select test.name,test2.name2 from test left join test2 on test.id test2.id 2.select test.name,test2.name2 from test right join test2 on test.id test...