问题描述
如果我有两个表,例如:
If I have two tables, like this for example:
表 1(产品)
id
name
price
agentid
表2(代理)
userid
name
email
如何从包含代理名称和电子邮件的产品中获取结果集,这意味着 products.agentid = agent.userid?
How do I get a result set from products that include the agents name and email, meaning that products.agentid = agent.userid?
我如何加入例如 SELECT WHERE price <100?
推荐答案
编辑以支持价格过滤
您可以使用 INNER JOIN 子句来连接这些表.它是这样完成的:
You can use the INNER JOIN clause to join those tables. It is done this way:
select p.id, p.name as ProductName, a.userid, a.name as AgentName
from products p
inner join agents a on a.userid = p.agentid
where p.price < 100
另一种方法是通过 WHERE 子句:
Another way to do this is by a WHERE clause:
select p.id, p.name as ProductName, a.userid, a.name as AgentName
from products p, agents a
where a.userid = p.agentid and p.price < 100
请注意,在第二种情况下,您正在对两个表中的所有行进行自然乘积,然后过滤结果.在第一种情况下,您在加入同一步骤时直接过滤结果.DBMS 将了解您的意图(无论您选择以何种方式解决此问题)并以最快的方式处理.
Note in the second case you are making a natural product of all rows from both tables and then filtering the result. In the first case you are directly filtering the result while joining in the same step. The DBMS will understand your intentions (regardless of the way you choose to solve this) and handle it in the fastest way.
这篇关于在 Select 中合并两个表 (SQL Server 2008)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)