如何在同一查询中选择MySQL表行数据作为列?

时间:2019-05-16 18:34:45

标签: mysql

如何在同一查询中选择MySQL表行数据作为列?

我必须编写一个用户导出到.csv工具,需要弄清楚如何从一个表(user_preferences)中获取数据作为查询中的列,并从另一个表(user_preference_values)中填充这些列,结构:

MariaDB> describe modx_user_preferences;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| preference | varchar(50)      | YES  |     | NULL    |                |
| alias      | varchar(50)      | YES  |     | NULL    |                |
| type       | varchar(50)      | YES  |     | NULL    |                |
| menuindex  | int(10)          | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

MariaDB> describe modx_user_preference_values;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| internalKey | int(10) unsigned | NO   | MUL | NULL    |                |
| preference  | varchar(50)      | NO   |     | NULL    |                |
| priority    | varchar(50)      | NO   |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

这些表需要连接到用户表:

MariaDB> describe modx_users;
+---------------+---------------------+------+-----+-------------------+----------------+
| Field         | Type                | Null | Key | Default           | Extra          |
+---------------+---------------------+------+-----+-------------------+----------------+
| id            | int(10) unsigned    | NO   | PRI | NULL              | auto_increment |
| username      | varchar(100)        | NO   | UNI |                   |                |
| password      | varchar(255)        | NO   |     |                   |                |
| cachepwd      | varchar(255)        | NO   |     |                   |                |
| class_key     | varchar(100)        | NO   | MUL | modUser           |                |
| active        | tinyint(1) unsigned | NO   |     | 1                 |                |
| remote_key    | varchar(255)        | YES  | MUL | NULL              |                |
| remote_data   | mediumtext          | YES  |     | NULL              |                |
| hash_class    | varchar(100)        | NO   |     | hashing.modNative |                |
| salt          | varchar(100)        | NO   |     |                   |                |
| primary_group | int(10) unsigned    | NO   | MUL | 0                 |                |
| session_stale | mediumtext          | YES  |     | NULL              |                |
| sudo          | tinyint(1) unsigned | NO   |     | 0                 |                |
| createdon     | int(20)             | NO   |     | 0                 |                |
+---------------+---------------------+------+-----+-------------------+----------------+
14 rows in set (0.00 sec)

和需要从user_preference表中选择为列的数据:

    MariaDB> select preference from modx_user_preferences;
    +----------------------------------------+
    | preference                             |
    +----------------------------------------+
    | Hematologist                           |
    | Surgical Oncologist                    |
    | Radiation Oncologist                   |

... there are about 50 of them ...

    | Not Applicable                         |
    | Medical Oncology                       |
    +----------------------------------------+
    33 rows in set (0.00 sec)

所以基本上,我需要将所有这些行都选择为查询中的列,并从user_preference_values表的数据中填充(布尔值)

如何/可以在同一查询中完成?

0 个答案:

没有答案