❶ 求解,两张表连接,是一对多的数据显示,要怎么写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、多对多:比如说一个班级有很多学生,他们有语文课、数学课、英语课等很多课。一门课有很多人上,一个人上很多门课。这里学生和课程的关系就是多对多。