1.一張表中有使用者資訊表user(user_id,nickname),另外一張表聯絡人表contact(user_id,friend_id).現在要找出聯絡人的資訊
1
select
u1.nickname
as
myselft,u2.nickname
as
friend
from
contact c
inner
join
user
u1
on
c.user_id=u1.user_id
inner
join
user
u2
on
c.friend_id=u2.id
以上如果friend_id可能為空的話,則後面那個inner join 改為left join.
2.查詢出好友及自己發的帖子,帖子表invitation(id,owner_id,title,content)
1
2
3
4
select
*
from
( invitation i
inner
join
user
u
on
u.user_id=i.owner_id )
inner
join
(
select
friend_id
from
contacts
where
user_id=2
union
all
select
2)
as
b
on
b.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 ...