Ⅰ sql的一對多,多對一,一對一,多對多什麼意思
1、一對多:比如說一個班級有很多學生,可是這個班級只有一個班主任。在這個班級中隨便找一個人,就會知道他們的班主任是誰;知道了這個班主任就會知道有哪幾個學生。這里班主任和學生的關系就是一對多。
2、多對一:比如說一個班級有很多學生,可是這個班級只有一個班主任。在這個班級中隨便找一個人,就會知道他們的班主任是誰;知道了這個班主任就會知道有哪幾個學生。這里學生和班主任的關系就是多對一。
3、一對一:比如說一個班級有很多學生,他們分別有不同的學號。一個學生對應一個學號,一個學號對應一個學生;通過學號能找到學生,通過學生也能得到學號,不會重復。這里學生和學號的關系就是一對一。
4、多對多:比如說一個班級有很多學生,他們有語文課、數學課、英語課等很多課。一門課有很多人上,一個人上很多門課。這里學生和課程的關系就是多對多。
Ⅱ sql 一對多統計查詢
借一下1L的語句
select u.id,u.niceName,u.imei,u.address,u.mobile,v.次數
from users as u ,
(select count(1) 次數,imei from reback Where [time]>'2012-11-06'
and [time]<'2012-11-07' group by imei ) v
where v.imei = u.imei
order by v.次數 desc
然後順便一提,4000w的表,那麼time、imei兩列都要加索引,還有users表中的imei也要加索引
Ⅲ sql如何一對多聯合查詢
sql 語句是這樣寫:
sql="select time,content,b.classname from record a left join class b on a.class_id=b.id"
下面是在ASP頁面用來顯示的:(創建那個與數據連接的對象不用寫了吧)
<TABLE>
<%
set rs=conn.execute(sql) 執行上面的語句
do while not rs.eof
%>
<TR>
<TD><%=rs("time")%></TD>
<TD><%=rs("content")%></TD>
<TD><%=rs("classname ")%></TD>
</TR>
<%
rs.movenext
loop
%>
</TABLE>
如果你是oracle中:
select 人名,sum(sale) from (select a.id,a.sale,b.人名 from s a,p b where a.who=b.id(+)) group by 人名
在sql 和access:
select 人名,sum(sale) from (select a.id,a.sale,b.人名 from s a left join p b on a.who=b.id(+)) group by 人名
其中 a,b 是表s,表p的別名
Ⅳ sql 一對多的查詢
你怎麼倆table1,後一個叫table2吧
執行以下語句
selectt1.考試學號,t1.科目,t2.考試成績
from
(selecta.科目,a.科目ID,b.考試學號
from
(select科目,科目IDfromtable1)asa,
(selectdistinct考試學號fromtable2)asb)ast1leftjoin
table2ast2ont1.科目ID=t2.科目IDandt1.考試學號=t2.考試學號
結果截圖
下邊還有幾條沒顯示全,湊合看吧
Ⅳ sql 一對多查詢
交給我吧,典型的交叉查詢,行列轉換
select s.StudentName,
(case r.Subject when '語文' then r.Math end) as 語文,
(case r.Subject when '數學' then r.Math end) as 數學,
(case r.Subject when '英語' then r.Math end) as 英語,
(case r.Subject when '物理' then r.Math end) as 物理,
(case r.Subject when '地理' then r.Math end) as 地理
from Student s,Results r
where s.StudentId=r.StudentId
能用的話記得給分哦~
Ⅵ SQL一對多查詢問題
select t1.學號,t1.姓名,t1.性別,t2.科目,t2.成績 from 總表 t1,分表 t2 where t1.學號=t2.學號 order by t1.學號
另你說的跨行顯示如果是值相同就合並上下兩個單元格的意思的話,是可以的,要在後台寫代碼合並單元格。
protected void GridView2_DataBound(object sender, EventArgs e)
{
int inArA;
int inArB;
int intspan;
string temp="",temp1="";
for (inArA = 0; inArA < GridView2.Rows.Count; inArA++)
{
GridViewRow _itm = GridView2.Rows[inArA];
intspan = 1;
temp = GridView2.Rows[inArA].Cells[0].Text;
#region IP不同導致出現兩條記錄時,開始合並
for (inArB = inArA + 1; inArB < GridView2.Rows.Count; inArB++)
{
temp1 = GridView2.Rows[inArB].Cells[0].Text;
if (String.Compare(temp, temp1) == 0)
{
intspan++;
//GridView2.Rows[inArA].Cells[0].RowSpan = intspan;//學號合並
//GridView2.Rows[inArA].Cells[1].RowSpan = intspan;//姓名合並
//GridView2.Rows[inArA].Cells[2].RowSpan = intspan;//性別合並
GridView2.Rows[inArA].Cells[3].RowSpan = intspan;//科目不合並
GridView2.Rows[inArA].Cells[4].RowSpan = intspan;//成績不合並
//GridView2.Rows[inArB].Cells[0].Visible = false;
//GridView2.Rows[inArB].Cells[1].Visible = false;
//GridView2.Rows[inArB].Cells[2].Visible = false;
GridView2.Rows[inArB].Cells[3].Visible = false;
GridView2.Rows[inArB].Cells[4].Visible = false;
}
else
{
break;
}
}
#endregion
inArA = inArB - 1;
}
}
希望對你有幫助
Ⅶ sql一對多查詢
select客戶表.客戶編號,nvl(stat.c,0)
from客戶表
leftjoin(select客戶編號,sum(商品價格)cfrom交易表groupby客戶編號)statonstat.客戶編號=客戶表.客戶編號
Ⅷ sql一對多的查詢代碼怎麼查
代碼如下:
SELECT a.trumbnail, b.url, c.url
FROM Goods a, Picture b, Picture c
WHERE a.picture=b.id AND a.picture=c.id