简体   繁体   English

MySQL:根据另一个表中的值过滤选择的值

[英]MySQL : Filter selected value based on value from another table

I have a table as following: 我有一张桌子,如下所示:

User tables 用户

[id] | [name] | [gender] | [phone]    
1    | David  | M        | 12345678 
2    | Mary   | F        | 18345679 
3    | Joe    | M        | 12845670 
4    | John   | M        | 12345671 
5    | May    | F        | 16355672 

And another table 还有另一张桌子

Access_Control tables Access_Control

[company] | [access_allowed]
Company A | gender 
Company A | phone
Company A | name
Company B | name
Company C | gender
Company C | phone

Question : A want a view like the following only using SQL 问题 :仅使用SQL即可获得类似以下的视图

[uid]| [name] | [gender] | [phone]  | [owned]
1    | David  | M        | 12345678 | Company A
2    | Mary   | F        | 18345679 | Company A
3    | Joe    | M        | 12845670 | Company A
4    | John   | M        | 12345671 | Company A
5    | May    | F        | 16355672 | Company A  
1    | David  | N/A      | N/A      | Company B
2    | Mary   | N/A      | N/A      | Company B
3    | Joe    | N/A      | N/A      | Company B
4    | John   | N/A      | N/A      | Company B
5    | May    | N/A      | N/A      | Company B 
1    | N/A    | M        | 12345678 | Company C
2    | N/A    | F        | 18345679 | Company C
3    | N/A    | M        | 12845670 | Company C
4    | N/A    | M        | 12345671 | Company C
5    | N/A    | F        | 16355672 | Company C 

Access_Control tables schema can be changed if needed. 如果需要,可以更改Access_Control表架构。

This is easy to do with conjunction to a server side language, but is this possible with just SQL? 结合服务器端语言很容易做到这一点,但是仅使用SQL就能做到吗? Thanks! 谢谢!

After 3 months, I am finally able to answer my own question, the solution is a bit dirty but it does the job and run very fast. 三个月后,我终于可以回答我自己的问题了,该解决方案虽然有点脏,但确实可以完成并且运行非常快。


Step 1: Change the Access_Control to a vertical view 步骤1:将Access_Control更改为垂直视图

CREATE VIEW `access_control_vertical` AS
    SELECT 
        `access_control`.`company` AS `company`,
        BIT_OR((CASE
            WHEN (`access_control`.`access_allowed` = 'gender') THEN 1
            ELSE 0
        END)) AS `gender`,
        BIT_OR((CASE
            WHEN (`access_control`.`access_allowed` = 'name') THEN 1
            ELSE 0
        END)) AS `name`,
        BIT_OR((CASE
            WHEN (`access_control`.`access_allowed` = 'phone') THEN 1
            ELSE 0
        END)) AS `phone`
    FROM
        `access_control`
    GROUP BY `access_control`.`company`

Result 1: 结果1:

company gender name phone
A       1       1   1
B       0       1   0
C       1       0   1

Step 2: Use cases to filter out which company can have which parameter from user 第2步:用例从用户中过滤出哪个公司可以拥有哪个参数

CREATE VIEW `filtered_user` AS
    SELECT 
        `access_control_vertical`.`company` AS `company`,
        (CASE
            WHEN (`access_control_vertical`.`gender` = 1) THEN `user`.`gender`
            ELSE 'N/A'
        END) AS `gender`,
        (CASE
            WHEN (`access_control_vertical`.`gender` = 1) THEN `user`.`name`
            ELSE 'N/A'
        END) AS `name`,
        (CASE
            WHEN (`access_control_vertical`.`gender` = 1) THEN `user`.`phone`
            ELSE 'N/A'
        END) AS `phone`
    FROM
        (`user`
        JOIN `access_control_vertical` ON ((1 = 1)))

Result 2: 结果2:

company, gender, name, phone
'A', 'M', 'David', '12345678'
'A', 'F', 'Mary   ', '18345679'
'A', 'M', 'Joe    ', '12845670'
'A', 'M', 'John   ', '12345671'
'A', 'F', 'May    ', '16355672'
'B', 'N/A', 'N/A', 'N/A'
'B', 'N/A', 'N/A', 'N/A'
'B', 'N/A', 'N/A', 'N/A'
'B', 'N/A', 'N/A', 'N/A'
'B', 'N/A', 'N/A', 'N/A'
'C', 'M', 'David', '12345678'
'C', 'F', 'Mary   ', '18345679'
'C', 'M', 'Joe    ', '12845670'
'C', 'M', 'John   ', '12345671'
'C', 'F', 'May    ', '16355672'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据从另一个表中选择的值进行限制 - Limiting based on value selected from another table MySQL触发器-使用从另一个表中选择的值更新表 - MySQL Trigger - update table with value selected from another table mysql插入值,从另一个表中选择数据 - mysql insert with value, with selected data from another table 如何将下拉列表中的选定值插入另一个 MySQL 表 - How to insert selected value from dropdown list into another MySQL table MYSQL根据另一个表中的值写入表 - MYSQL Write to a table based on a value in another table 根据Mysql中的另一个表值更新表 - Update a table based on another table value in Mysql 如何在一个 select 选项中获取选定值,并在此基础上,从 MySQL 表中获取数据,以相同形式显示另一个 select 选项 - How to get selected value in one select option and based on that, fetch data from MySQL table to show another select option in the same form mysql根据另一个表中的值删除行 - mysql delete row based on value from another table MySQL select 一个表中的值并根据第一个表中的值对另一个表中的值求和 - MySQL select a value from a table and sum values on another table based on the value from the first table 如果 mySQL 中的选定值为“0”,则来自另一列的 Select - Select from another column if selected value is '0' in mySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM