在此先感谢,我有两个表,一个是用户表,另一个是检查表。 users表包含所有用户列表和检查表包含用户检查的所有数据,因此它包含具有不同管理的行。那么如果检查表包含至少一个adminid,如何编写一个sql查询来获取用户名。
我的用户表:
id |username |Phone |email |role
1 | Girish |9834343468|girish@yahoo.com |admin
2 | Rakesh |9434343432|rakesh@gmail.com |admin
3 | Suresh |9434343772|suresh@gmail.com |developer
检查表:
id | userid | username | admin_id |order_id |exam |
1 | 3 | suresh | 1 | df54545454fg09|
2 | 3 | suresh | 1 | df54545454fg09|
3 | 3 | suresh | 2 | gh54995454fg55|
我需要一个sql查询,其中至少有一个" admin_id"检查表包含用户表" id"
我想创建一个像这样的表
id|admin_id|name |email |Phone
1 | 1 |Girish |girish@gmail.com |9834343468
2 | 2 |Rakesh |rakesh@gmail.com |9434343432
答案 0 :(得分:0)
试试这个:
select u.id,a.admin_id,u.username name, u.email, u.Phone
from users u
(select c.admin_id
from checks c
group by c.admin_id) a
where u.id=a.admin_id
答案 1 :(得分:0)
SELECT * FROM User_table WHERE EXISTS (SELECT 1 FROM admin_table WHERE id = admin_id)
试试这个 SELECT id,admin_id,A.username name,email,Phone 来自User_table 加入 ( SELECT DISTINCT Admin_Id,用户名 来自admin_table )A A.Amin_Id = id
答案 2 :(得分:0)
尝试以下查询
SELECT u.id,a.admin_id,u.username name, u.email, u.Phone
FROM users u,
(SELECT DISTINCT admin_id
FROM checks c) a
WHERE u.id=a.admin_id
这可能会对你有帮助。