从多个表中选择数据 [英] pick data from multiple table

查看:80
本文介绍了从多个表中选择数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,我需要一些帮助
在我的项目(asp.net)中,sqlserver 2005是后端,我需要查询从多个表中检索数据
对于例如
table1:traindetail(tid(p.key),名称,来​​源,目的地,日期)
table2:class(tid(f.key),class,seat)
我有一个表格收集用户输入的信息,例如来源,目的地,类别,日期
当用户提交时,它将检查两个表并匹配,然后显示确认消息
如何获取它

hi friends i need some helps
In my project(asp.net) sqlserver 2005 is backend in this i need query for retrive data from multiple table
for eg
table1:traindetail(tid(p.key),name,source,destination,date)
table2:class(tid(f.key),class,seat)
i have one form in i collect users input like source,destination,class,date
when user submit then it check the two tables and it match then display conformation message
how can i get it

推荐答案

使用联接
Select source, destination, class, [date] 
from table1 
  join table2 on table1.tid = table2.tid


SQL联接 [


SQL Joins[^]


您要的是最简单的查询之一.
您必须使用JOINS来实现此目的.
如果要获取有关JOINS的信息,请搜索GOOGLE.
You are asking for one of the simplest queries.
You have to use JOINS for achieveing this.
Search GOOGLE if you want to get information about JOINS.


select A.tid, A.name, B.class, B.seat from traindetail as A inner join class as B on A.tid=B.tid where A.source = 'SourceFromUser' and A.destination = 'DestinationFromUser' and A.date=convert(datetime,'DateFromUser',103) and B.class='classFromUser'



执行此查询并检查行数.如果行数是> 0,然后显示构型,否则显示适当的错误消息.


希望这会有所帮助.



execute this query and check the row count. if the rowcount is > 0 then display conformation otherwise display appropriate error message.


hope this helps..


这篇关于从多个表中选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆