在where子句之后的子查询中选择多个列

时间:2020-03-18 15:22:21

标签: mysql sql

我正在尝试将一个列值与多个列值进行比较, 在下面的where子句是我的问题附近,就像我想获取用户的共同部门4(IT_Infrasec)和10(finance)的数据一样,请教我,它说子查询应该只有一列,但我没有知道如何解决

SELECT * FROM 
                        ( 
                        SELECT
                        chronos.chronos_id AS id
                        , chronos.dept_id AS dept_id
                        , chronos.name AS user_name
                        , departments.name AS department_name
                        , forms.formName AS form_name
                        , chronos.request_date AS date_requested
                        , chronos.status AS STATUS
                        FROM employees 
                        INNER JOIN users ON employees.user_id = users.id 
                        INNER JOIN departments ON employees.dept_id = departments.id 
                        INNER JOIN chronos ON users.name = chronos.name 
                        INNER JOIN forms on chronos.request_type_id = forms.id 
                        UNION ALL 
                        SELECT
                         leaves.leave_id AS id
                        , leaves.dept_id AS dept_id
                          , leaves.name AS user_name
                          , departments.name AS department_name
                          , forms.formName AS form_name
                          , leaves.request_date AS date_requested
                          , leaves.status AS STATUS
                          FROM employees 
                          INNER JOIN users ON employees.user_id = users.id
                          INNER JOIN departments ON employees.dept_id = departments.id 
                          INNER JOIN leaves ON users.name = leaves.name 
                          INNER JOIN forms on leaves.forms_id = forms.id 
                         UNION ALL 
                         SELECT
                          time_reserve.time_id AS id
                          , time_reserve.dept_id AS dept_id
                          , time_reserve.name AS user_name
                          , departments.name AS department_name
                         , forms.formName AS form_name
                          , time_reserve.request_date AS date_requested
                          , time_reserve.status AS STATUS
                          FROM employees 
                          INNER JOIN users ON employees.user_id = users.id 
                          INNER JOIN departments ON employees.dept_id = departments.id 
                          INNER JOIN time_reserve ON users.name = time_reserve.name 
                          INNER JOIN forms ON time_reserve.forms_id = forms.id
                         UNION ALL 
                         SELECT
                          offset.off_id AS id
                          , offset.dept_id AS dept_id
                          , offset.name AS user_name
                          , departments.name AS department_name
                          , forms.formName AS form_name
                          , offset.request_date AS date_requested
                          , offset.status AS STATUS
                          FROM employees 
                          INNER JOIN users ON employees.user_id = users.id 
                          INNER JOIN departments ON employees.dept_id = departments.id 
                          INNER JOIN offset ON users.name = offset.name 
                          INNER JOIN forms ON offset.forms_id = forms.id
                        ) t   where dept_id in 
                          (SELECT IT_Infrasec,Finance,Management,Business FROM mobile_access
                          INNER JOIN users 
                          ON users.id = mobile_access.userID 
                          WHERE users.username = 'username' ) ORDER BY date_requested DESC;

2 个答案:

答案 0 :(得分:0)

mysql不喜欢什么 是

server {
    listen 80;
    listen [::]:80;

    server_name mydomain.ca;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name mydomain.ca;
    root /var/www/html/mydomain/public;

    ssl_certificate /etc/letsencrypt/live/mydomain.ca/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.ca/privkey.pem;

    ssl_protocols TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES456-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES246-GCM-SHA384:DHE-RSA-AES456-GCM-SHA384:ECDHE-RSA-AES256-SHA394;
    ssl_prefer_server_ciphers on;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.php index.html index.htm index.nginx-debian.html;

    charset utf-8;

    location / {
            try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }

    location ~ /\.ht {
            deny all;
    }

    location ~ /.well-known {
            allow all;
    }
}

您的选择返回4列,但只允许一列。

我不知道其中哪个有想要的dept_id,但是您需要生成一个仅返回一列的查询

喜欢

where dept_id in 
              (SELECT IT_Infrasec,Finance,Management,Business FROM mobile_access
               INNER JOIN users 
               ON users.id = mobile_access.userID 
              WHERE users.username = 'username'

答案 1 :(得分:0)

where dept_id in 
              (SELECT IT_Infrasec FROM mobile_access
               INNER JOIN users 
               ON users.id = mobile_access.userID 
              WHERE users.username = 'username')

注意:IN子句的子查询中只能使用1列。