-3

我有两张表,一张名为logs另一张名为location.

logs中,我有以下内容:

___________________________
| id | message | location |
'-------------------------'
| 1  | test    | 1        |
| 2  | test    | 2        |
| 3  | test    | 1        |
| 4  | test    | 1        |
___________________________

location表中,我有这个:

_________________
| id | location |
'---------------'
| 1  | US       |
| 2  | UK       |
_________________

我想用我的查询输出的是:

___________________________
| id | message | location |
'-------------------------'
| 1  | test    | US       |
| 2  | test    | UK       |
| 3  | test    | US       |
| 4  | test    | US       |
___________________________

我怎么做?

4

5 回答 5

1
SELECT logs.id, logs.message,location.location 
from logs, location 
where logs.location= location.location 

此查询中应用的联接是内部联接。

于 2013-05-24T06:34:43.847 回答
1

尝试这个 :

select lg.id, lg.message, lo.location 
from logs lg, location lo 
where lg.location = lo.id
于 2013-05-24T06:30:50.093 回答
0
 select a.id,a.message,b.location 
 from 
 logs a , location b 
 where 
 logs.location = location.id;

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

于 2013-05-24T06:30:09.120 回答
0

使用下面的查询。

SELECT a.id, a.message, b.location 
      FROM logs a 
           LEFT JOIN location b 
           ON a.location = b.id;
于 2013-05-24T06:30:13.967 回答
0

尝试这个

 SELECT lo.id,message,location
 FROM logs lo ,location l
 WHERE lo.location = l.id
于 2013-05-24T06:30:53.500 回答