我有两张桌子:
[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;
答案 0 :(得分:4)
对于用户名杰克如下:
select TABLE1.USERNAME, TABLE1.POST
from TABLE1, TABLE2
where
TABLE1.USERNAME = TABLE2.FOLLOWING
and
TABLE2.USERNAME = "Jack"
对于关注杰克的用户名
select TABLE1.USERNAME, TABLE1.POST
from TABLE1, TABLE2
where
TABLE1.USERNAME = TABLE2.USERNAME
and
TABLE2.FOLLOWING = "JACK"
答案 1 :(得分: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
条件
答案 2 :(得分:0)
这有帮助吗?
SELECT * FROM table1 t1, table2 t2
WHERE t2.username = 'Jack'
AND t1.username = t2.following