SQL語句:多表關聯查詢
在SQL里,常常需要對多個表關聯起來進行查詢,下面把我寫的一個簡單的多表關聯的例子給大家看看,方法很簡單,只要你學會原理就行:
select
* id,* oid,* number,*r seOrder,*ce endprice,--第一個表的字段
* uid,*ime oDatetime,--第二個表的字段
*e proname,* spec,*al material,* price,--第三個表的字段
*1 price1,*2 price2,*3 price3,*4 price4,*5 price5 --第四個表的字段
from
orderlist o --表一
left join products p on *=* --表二
left join orderForm d on *=* --表三
left join classify c on *=* --表四
--更多的表
order by * desc
這樣,就把四個表關聯起來查詢了。如果有更多的表,可以一個一個的關聯下去,不過我還是不希望關聯的表太多.
多表關聯查詢語法
這里舉例多表關聯的例子,只要學會原理就行:select * id,* oid,* number,*r seOrder,*ce endprice,--第一個表的字段* uid,*ime oDatetime,--第二個表的字段*e proname,* spec,*al material,* price,--第三個表的字段*1 price1,*2 price2,*3 price3,*4 price4,*5 price5 --第四個表的字段from orderlist o --表一left join products p on *=* --表二left join orderForm d on *=* --表三left join classify c on *=* --表四--更多的表order by * desc這樣,就把四個表關聯起來查詢了。
如果有更多的表,可以一個一個的關聯下去。
多表關聯,SQL語句查詢,求幫助
create table A(AID) --類型略
create table B(AID,EID)
create table C(CID)
create table D(CID,EID)
create table E(EID)
select * from E where EID in (select EID from B where AID in (select AID from A where AName='abc'))
and EID in (select EID from D where CID in (select CID from C where CName='xyz'))
SQL 一對多表關聯查詢
補充:
。.這個,感覺樓主的語句不用寫成這樣詭異的,你可以試試下面的語句符不符合你的要求
select * from table1 a,table 2 b where *=*;
這樣不就行了么??
可以給多一點信息么??具體你想返回什么呢??
是這樣么?
table1符合的記錄字段 table1所對應的很多table2
的中的一個記錄 字段
** ++++++
** ++++++
** ++++++
** ++++++
如果是這樣的話,table2應該是存儲有關交易的信息的吧,那么應該有一個唯一標識的編號,類型為數值,越新的記錄,編號越大,設這個編號為id 那么可以用以下方法,達到你想要的效果
select * from table1,table2 where * in (
select max(id) from table2 group by name) and *
=*;
強烈建議不要使用日期字段作唯一標識,如果沒有以上所說的id列,會麻煩許多