我有两张桌子如下。
表val1
e_id name val
1 A1 Abc
2 A2 Abd
3 A3 AbE
4 A4 AEd
5 A5 AEd
6 A6 Bdc
表val2
e_id e_desc t_id
1 desc1 1
2 desc1 1
1 desc1 2
3 desc1 1
5 desc1 1
2 desc1 2
4 desc1 2
5 desc1 2
2 desc1 3
4 desc1 1
6 desc1 1
3 desc1 2
6 desc1 2
3 desc1 2
5 desc1 3
我想通过e_id map获取表Val1中的值,表Val2上的e_id为表Val2 t_id = 1
我正在使用此查询,但它正在获取所有数据。我该如何解决这个问题。这是我在下面显示的sql代码
SELECT
a.*,
b.e_desc
FROM
val1 AS a, val2 AS b
WHERE
b.e_id = a.e_id
AND EXISTS (SELECT
c.e_id
FROM val2 AS c
WHERE
c.e_id = a.e_id
AND c.t_id='1'
)
答案 0 :(得分:2)
如何
SELECT a.*,
b.e_desc
FROM val1 AS a,
val2 AS b
WHERE b.e_id = a.e_id
AND b.t_id='1'
甚至
SELECT a.*,
b.e_desc
FROM val1 AS a INNER JOIN
val2 AS b ON b.e_id = a.e_id
WHERE b.t_id='1'
答案 1 :(得分:1)
可以通过JOIN
两个表来解决这个问题。试试这个,
SELECT a.*, b.e_desc
FROM tableA a
INNER JOIN tableB b
ON a.e_id = b.e_id AND
a.e_id = b.t_id
WHERE b.t_id = 1