求大侠帮我写一条sql语句啊,我想了半天了,求助
三张表,
一张user_profile用户表,
user_manage上下级管理表,
weekly_report用户周报表,
user_manage里面的两个字段都是user_profile里面的id,
weekl_report里面有个user_profile的id字段,
查询出user_manage里面的当前管理者的下级的周报,也要查出当前管理者自己的周报
user_profile的user_oid是主键
mysql> desc user_profile;
+----------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------+------+-----+---------+-------+
| USER_OID | decimal(38,0) | NO | PRI | NULL | |
| USER_NAME | varchar(50) | NO | | NULL | |
| DISPLAY_NAME | varchar(50) | NO | | NULL | |
| LOGIN_ID | varchar(20) | NO | | NULL | |
| LOGIN_PASSWORD | varchar(150) | NO | | NULL | |
| EMAIL | varchar(100) | NO | | NULL | |
| IS_ACTIVE | char(1) | NO | | NULL | |
| CREATE_DATE | datetime | NO | | NULL | |
| UPDATE_DATE | datetime | YES | | NULL | |
| ACTOR | varchar(20) | NO | | NULL | |
| ACTION_TYPE | varchar(20) | NO | | NULL | |
| ACTION_DATE | datetime | NO | | NULL | |
+----------------+---------------+------+-----+---------+-------+
12 rows in set (0.02 sec)
user_manage里面的manage_user与employee_user都是user_profile里面的主键
mysql> desc user_manage;
+---------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+-------+
| MANAGER_USER | decimal(38,0) | NO | MUL | NULL | |
| EMPLOYEE_USER | decimal(38,0) | NO | MUL | NULL | |
+---------------+---------------+------+-----+---------+-------+
2 rows in set (0.02 sec)
weekly_reprot里面的reprot_oid是主键,user_oid是user_profile里面的主键
mysql> desc weekly_report;
+-------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| REPORT_OID | decimal(38,0) | NO | PRI | NULL | |
| REPORT_NAME | varchar(100) | NO | | NULL | |
| FROM_DATE | datetime | NO | | NULL | |
| END_DATE | datetime | NO | | NULL | |
| USER_OID | decimal(38,0) | NO | MUL | NULL | |
| CTRL_STATUS | char(1) | NO | | NULL | |
| CREATE_DATE | datetime | NO | | NULL | |
| UPDATE_DATE | datetime | YES | | NULL | |
| ACTOR | varchar(32) | NO | | NULL | |
| ACTION_TYPE | varchar(20) | NO | | NULL | |
| ACTION_DATE | datetime | NO | | NULL | |
+-------------+---------------+------+-----+---------+-------+
11 rows in set (0.03 sec)
------解决方案--------------------
你这个当前管理者是一个输入吗?就当是一个需要输入的值吧。另外你这个问题是要一个语句查出还是分两个语句做?
你参考一下:
查询出 user_manage里面的当前管理者的下级的周报:
SQL code
select T1.*
from weekly_report as T1 left join user_manage as T2
on T1.user_oid = T2.employee_user
where T2.manage_user = <当前管理者的ID>