如何在sqlserver中编写查询

如何在sqlserver中编写查询

本文介绍了如何在sqlserver中编写查询以加入两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表名UserInfromation

列名称数据类型允许为空
UserId Int(Set Identity = true)否
用户名varchar(50)是
位置Varchar(50)是


表名称OrderInformation

列名称数据类型允许为空
OrderId Int否
OrderNo int是
UserId int NO


UserInfromation表数据如下;
UserId用户名位置
1苏雷什·钦奈
2 Prasant Chennai
3德里Madhavi
4 Mahesh孟买

OrderInformation表数据如下;
OrderId OrderNo用户ID
1 543 1
2213 2
3 977 3
4323 3
5 998 1


我的输出如下所示

UserId用户名位置OrderNo
1 Suresh Chennai 543
1 Suresh Chennai 998
2 Prasant Chennai 213
3马德里Madhavi 977
3 Madhavi Delhi 323


对于上面的查询,如何在sqlserver中编写查询.请问我,我是第一次使用联接编写查询.

Table Name UserInfromation

Column Name Data Type Allow Nulls
UserId Int (Set Identity=true) No
UserName varchar(50) Yes
Location Varchar(50) Yes


Table Name OrderInformation

Column Name Data Type Allow Nulls
OrderId Int No
OrderNo int Yes
UserId int NO


UserInfromation table data as follows;
UserId UserName Location
1 Suresh Chennai
2 Prasant Chennai
3 Madhavi Delhi
4 Mahesh Mumbai

OrderInformation table data as follows;
OrderId OrderNo UserId
1 543 1
2 213 2
3 977 3
4 323 3
5 998 1


I wan the output as follows

UserId UserName Location OrderNo
1 Suresh Chennai 543
1 Suresh Chennai 998
2 Prasant Chennai 213
3 Madhavi Delhi 977
3 Madhavi Delhi 323


For the above query how to write the query in sqlserver.please hlep me i am new to write the query using joins.

推荐答案

SELECT u.UserId, u.UserName, u.Location, o.OrderNo
From UserInfromation u INNER JOIN OrderInformation o
ON u.UserId = o.UserId;



是您开始学习SQL JOIN的时候了.
http://www.w3schools.com/sql/sql_join.asp



Time for you to begin learning SQL JOINs.
http://www.w3schools.com/sql/sql_join.asp


select UserId, UserName, Location, OrderNo from  UserInfromation,OrderInfromation
where  UserInfromation.UserId= OrderInfromation.Userid



杜德(Dude),

我想您是Sql Server的初学者,因此请先通过任何书籍学习基础知识.

--prathap.



Dude,

i guess you are a beginner in Sql server, so first learn the basics through any book.

--Prathap.


这篇关于如何在sqlserver中编写查询以加入两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:47