幾種SQL語句的寫法

2021-09-20 23:42:19 字數 1286 閱讀 5614

1.一張表中有使用者資訊表user(user_id,nickname),另外一張表聯絡人表contact(user_id,friend_id).現在要找出聯絡人的資訊

1

selectu1.nicknameasmyselft,u2.nicknameasfriendfromcontact cinnerjoinuseru1onc.user_id=u1.user_idinnerjoinuseru2onc.friend_id=u2.id

以上如果friend_id可能為空的話,則後面那個inner join 改為left join.

2.查詢出好友及自己發的帖子,帖子表invitation(id,owner_id,title,content)

1

2

3

4

select*from( invitation iinnerjoinuseruonu.user_id=i.owner_id )

innerjoin

(selectfriend_idfromcontactswhereuser_id=2unionallselect2)asb

onb.friend_id=i.user_id

在mysql中就不要使用子查詢了,mysql 5.5以前的版本對子查詢效率極差。

sql語句有幾種寫法

sql語句有幾種寫法 1 select from tablename order by rand limit 想要獲取的資料條數 2 select from table where id select floor max id rand from table order by id limit 想要...

幾種使用sql寫法

q表示式可以處理換行 單引號等特殊字元 update t sys res config sql t set t.query sql q long string where t.bm class id t rc com internate config update語句 可以對查詢結果進行update...

查詢分頁的幾種Sql寫法

1.概述 在網頁中如果顯示的資料太多就會佔據過多的頁面,而且顯示速度也會很慢。為了控制每次在頁面上顯示資料的數量,就可以利用分頁來顯示資料。2.技術要點 select top pagesize from table where id not in select top prenum id from ...