0

我有两张桌子:

[TABLE1]

+----------+-------------------+
| USERNAME | POST              |
+----------+-------------------+
| Bob      | 'Hi There'        |
| Jack     | 'Hello'           |
| Bob      | 'Today is Monday' |
+----------+-------------------+


[TABLE2]

+----------+-----------+
| USERNAME | FOLLOWING |
+----------+-----------+
| Mike     | Jack      |
| Jack     | Bob       |
| Bob      | Jack      |
| Jack     | Mike      |
+----------+-----------+

我知道当你想要一行时如何连接两张桌子.. 但是当你想解决下面的问题时呢?

Select * from TABLE1 when TABLE1.USERNAME IS A USERNAME JACK is FOLLOWING;
4

3 回答 3

4

对于用户名 Jack 如下:

select TABLE1.USERNAME, TABLE1.POST 
from TABLE1, TABLE2
where 
     TABLE1.USERNAME = TABLE2.FOLLOWING 
     and
     TABLE2.USERNAME = "Jack"

对于关注 Jack 的用户名

select TABLE1.USERNAME, TABLE1.POST 
from TABLE1, TABLE2
where 
     TABLE1.USERNAME = TABLE2.USERNAME 
     and
     TABLE2.FOLLOWING = "JACK"
于 2012-07-23T09:25:05.440 回答
0

这有帮助吗?

SELECT * FROM table1 t1, table2 t2
WHERE t2.username = 'Jack'
AND t1.username = t2.following
于 2012-07-23T09:30:35.000 回答
0

尝试(假设 PHP)

$username - 'Jack';
"SELECT t1.* FROM TABLE1 t1
INNER JOIN table2 t2 
ON t1.username = t2.username AND t2.following = '".$username."' ";

注意:两个表的列名是否相同,那么您可以使用USING(username)而不是ON条件

于 2012-07-23T09:25:24.770 回答