SQL Server 2008中的SQL查询 [英] Sql query in SQL server 2008

查看:98
本文介绍了SQL Server 2008中的SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到谁的产品销售



输出我需要



i want to find the whose product items sold

output i need as

produtname    itemssold   (using product id)





如何在sql server中编写查询



我尝试了什么:





for that how to write a query in sql server

What I have tried:

productid   prodcutname  count
 1             A           2
 2             B           3
 3             C           1
 4             D           2
 5             E           1







prdouctname     itemssold
  A                1
  B                2
  C                0
  D                1
  E                1

推荐答案

这不会使很有意义:你在两个表中都没有ProductID,你有一个ProductName - 效率非常低,并且使你正在做的事情变得复杂。
尝试这些表:

That doesn't make a lot of sense: You don't have a ProductID in both tables, you have a ProductName - which is very inefficient as well as complicating what you are doing.
Try these tables:
ProductID	ProductName	Count
1	        A         	2
2	        B         	3
3	        C         	1
4	        D         	2
5	        E         	1




ProductID	ItemsSold
1	        1
2	        2
3	        0
4	        1
5	        1

此查询:

SELECT a.ProductName, b.ItemsSold FROM table1 a
JOIN table2 b ON a.ProductID = b.ProductID
WHERE b.ItemsSold > 0

这可以为您提供我认为您正在寻找的内容:

That gives you what I think you are looking for:

ProductName	ItemsSold
A         	1
B         	2
D         	1
E         	1


这篇关于SQL Server 2008中的SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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