加入两个tabales并使用WHERE condision获取右表值的左表中的所有值。
问题描述:
我有两个表Entity和EntityPoints
实体表
I have two tables Entity and EntityPoints
Entity table
EntityId | Name
1 | E1
2 | E2
3 | E3
EntityPoints表
EntityPoints Table
EntityId | QId |Points
1 | 1 |100
1 | 2 |200
2 | 2 |350
我的问题是使用这两个表如何使用实体表中的所有名称EntityPoints表的条件QId ...
我尝试使用以下代码,但它不包含所有实体名称表。我怎么能这样做?
my problem is using this two tables how to get all Names in Entity table using where condition for EntityPoints Table QId ...
I try with following code but it not contain all Names of Entity table.How can I do it ?
SELECT Entity.Name,Entity.EntityId,EntityPoints.QId,EntityPoints.Points
FROM Entity LEFT OUTER JOIN
EntityPoints ON Entity.EntityId = EntityPoints.EntityId
WHERE (EntityPoints.QId = 2)
添加了代码块 - OriginalGriff [/ edit]
[edit]Code block added - OriginalGriff[/edit]
答
尝试:
Try:
SELECT e.Name,e.EntityId,p.QId,p.Points
FROM Entity e
LEFT OUTER JOIN
(SELECT * FROM EntityPoints WHERE EntityPoints.QId = 2) p
ON e.EntityId = p.EntityId
在条件允许的情况下也使用它。
use this too in where condition..
OR EntityPoints.QId = NULL