我有一个名为Users
的表,其中有两个字段username
和password
。同样,我还有一个表,其中有几个字段和username
字段。
我有一个登录页面,我必须在其中检查username
表中是否存在password
和Users
,如果存在,我想从与该username
相对应的另一个表中获取数据。
单个用户可以有多个数据,因此我必须获取相应用户的所有数据。我该怎么做?
最佳答案
您可能要使用子查询...实际上是两个查询
mysql> select * from other
-> where name =
-> (select name from users where name = '<USER_NAME>' and password = '<PASSWORD>');
这是一个例子:
mysql> select * from users;
+------+------+----------+
| id | name | password |
+------+------+----------+
| 1 | one | one |
| 2 | two | two |
+------+------+----------+
2 rows in set (0.00 sec)
mysql> select * from other;
+------+-------+
| id | name |
+------+-------+
| 20 | two11 |
| 10 | two |
| 30 | two |
+------+-------+
3 rows in set (0.00 sec)
mysql> select * from other where name = (select name from users where name = 'two' and password = 'two');
+------+------+
| id | name |
+------+------+
| 10 | two |
| 30 | two |
+------+------+
2 rows in set (0.00 sec)