SQL巢狀查詢總結 瀟湘部落格

2021-04-20 01:04:51 字數 1360 閱讀 9045

it也有一段時間了,剛開始的時候``````

的困難主要是在程式語言上,陣列,邏輯,演算法,...

這些都過來了之後,困難就上公升到資料庫設計上了.

以及資料邏輯.

乙個優秀的系統,會整合優秀的程式和優秀的資料庫設計.

要做到這點得有足夠的經驗.

這是我寫的乙個結合uinon的巢狀查詢.

將五個方面的報表放到乙個臨時表裡,再從臨時表裡,將

所要的資料查詢出來.

$sql="select type , sum( yjsl ) as yjsl , sum( yysl ) as yysl, sum( jyrs ) as jyrs, sum( jycs ) as jycs

from (

select c.mc as

type , count( d.lsh ) as yjsl, 0 as yysl, 0 as jyrs, 0 as jycs

from sys_dzxxb as b, sys_jcb as c, sys_dzyjb as d

where b.bm = c.lsh

and d.dzlsh = b.lsh

group by c.mc

union select c.mc as

type , 0 as yjsl, count( e.lsh ) as yysl, 0 as jyrs, 0 as jycs

from sys_dzxxb as b, sys_jcb as c, sys_dzyy as e

where b.bm = c.lsh

and e.dzlsh = b.lsh

group by c.mc

union select c.mc as

type , 0 as yjsl, 0 as yysl, count( distinct e.dzlsh ) as jyrs, 0 as jycs

from sys_dzxxb as b, sys_jcb as c, sys_ltxxb as e

where b.bm = c.lsh

and e.dzlsh = b.lsh

group by c.mc

union select c.mc as

type , 0 as yjsl, 0 as yysl, 0 as jyrs, count( distinct e.lsh ) as jycs

from sys_dzxxb as b, sys_jcb as c, sys_ltxxb as e

where b.bm = c.lsh

and e.dzlsh = b.lsh

group by c.mc

) as temptable

group by type ";

分享給大家.

sql鑲嵌查詢 SQL巢狀查詢總結

it也有一段時間了,剛開始的時候 的困難主要是在程式語言上,陣列,邏輯,演算法,這些都過來了之後,困難就上公升到資料庫設計上了.以及資料邏輯.乙個優秀的系統,會整合優秀的程式和優秀的資料庫設計.要做到這點得有足夠的經驗.這是我寫的乙個結合uinon的巢狀查詢.將五個方面的報表放到乙個臨時表裡,再從臨...

SQL巢狀查詢

訂單表orders 顧客表 customers 訂單商品表orderitems 列出訂購物品rgan01的所有顧客 select cust name,cust contact from customers where cust id in select cust id from orders whe...

sql查詢之巢狀查詢

巢狀查詢也叫子查詢,乙個select語句的查詢結果能夠作為另外乙個語句的輸入值。子查詢不但可以出現在where子句中,也能出現在from中作為乙個臨時表使用,而且還可以出現在select list中,作為乙個欄位值來返回。1 單行子查詢 單行子查詢的返回值只有一行資料。可以再主查詢語句的條件語句中引...