我有一个名为Users
的表,其中有两个字段用户名
和密码
。同样,我还有一张表,其中有几个字段和 username
字段。
我有一个登录页面,我必须检查用户名
和密码
是否存在于Users
表中,如果存在,我想从与该用户名
对应的另一个表中获取数据。
单个用户可能有多个数据,因此我必须获取相应用户的所有数据。我该怎么做?
最佳答案
您可能想使用子查询...实际上是两个这样的查询
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)
关于mysql - 如何获取下面提到的条件下的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11877138/