❶ 求解,兩張表連接,是一對多的數據顯示,要怎麼寫sql語句
最好把表結構提供一下。
1、以「表名一」為基準,將「表名多」中「欄位」相同的行查出,「表名一」中有而「表名多」中不存在的就不顯示:
select 要查詢的欄位 from 表名一,表名二 where 表名一.欄位=表名多.欄位
2、以「表名一」為基準,將「表名多」中「欄位」相同的行查出,「表名一」中有的就都顯示,「表名多」中沒有的會顯示為「NULL」
select 要查詢的欄位 from 表名一
left outer join 表名多 on ( 表名一.欄位=表名多.欄位)
❷ 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語句,匹配多行數據的同一個欄位
什麼意思。。。?
select mainId
from mainTable
where mainId in (
select mainId from relationTable, tagTable
where relationTable.tagId = tagTable.tagId )
如果要特定的某個(某些)tagId,就增加條件
select mainId
from mainTable
where mainId in (
select mainId from relationTable, tagTable
where relationTable.tagId = tagTable.tagId
and tagTable.tagId = ?? ) //--或者 tagTable.tagId in (……)
❹ sql server連接表方式 一對多 如何實現
select a.id,b.value
from a left outer join b on a.id=b.id
❺ sql查詢,一對多的join用法
select top 5 * from a表
left join b表
on a表.id = b表.aid
where b表.id in (select Max(id) from b表 group by aid)
❻ 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的一對多,多對一,一對一,多對多什麼意思
1、一對多:比如說一個班級有很多學生,可是這個班級只有一個班主任。在這個班級中隨便找一個人,就會知道他們的班主任是誰;知道了這個班主任就會知道有哪幾個學生。這里班主任和學生的關系就是一對多。
2、多對一:比如說一個班級有很多學生,可是這個班級只有一個班主任。在這個班級中隨便找一個人,就會知道他們的班主任是誰;知道了這個班主任就會知道有哪幾個學生。這里學生和班主任的關系就是多對一。
3、一對一:比如說一個班級有很多學生,他們分別有不同的學號。一個學生對應一個學號,一個學號對應一個學生;通過學號能找到學生,通過學生也能得到學號,不會重復。這里學生和學號的關系就是一對一。
4、多對多:比如說一個班級有很多學生,他們有語文課、數學課、英語課等很多課。一門課有很多人上,一個人上很多門課。這里學生和課程的關系就是多對多。