MySQL替代

时间:2014-04-26 17:40:06

标签: mysql sql

        +-----------------------------+
        | tables                      |
        +-----------------------------+
        | Date                        |
        | IP                          |
        | Location                    |
        | UserAgent                   |
        +-----------------------------+

对于simplicty假设这四个表只有两列:ID(int),name(VARCHAR)

然后我有一个名为access_log的表,我只存储其他四个表中的项目ID

        +---------+---------+------+-----+---------+----------------+
        | Field   | Type    | Null | Key | Default | Extra          |
        +---------+---------+------+-----+---------+----------------+
        | id      | int(11) | NO   | PRI | NULL    | auto_increment |
        | ip_id   | int(11) | NO   |     | NULL    |                |
        | ua_id   | int(11) | NO   |     | NULL    |                |
        | path_id | int(11) | NO   |     | NULL    |                |
        | date_id | int(11) | NO   |     | NULL    |                |
        +---------+---------+------+-----+---------+----------------+

假设我想从此access_log表中选择所有内容,并将ID替换为我在其他四个表中的列NAME。 我该如何实现呢?

1 个答案:

答案 0 :(得分:0)

您需要加入所有四个表:

SELECT      access_log.id,
            `date`.name AS date_name, 
            ip.name AS ip_name, 
            location.name AS location_name, 
            useragent.name AS useragent_name
FROM        access_log
OUTER JOIN  `date` ON `date`.id = access_log.date_id
OUTER JOIN  ip ON ip.id = access_log.ip_id
OUTER JOIN  location ON location.id = access_log.path_id
OUTER JOIN  useragent ON useragent.id = access_log.ua_id