内连接 2 个具有相同列名的表 [英] Inner join 2 tables with same column names

查看:47
本文介绍了内连接 2 个具有相同列名的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力在我的网站上显示来自我的世界服务器的成就.但我无法让它工作.

I'm working on displaying the achievements from my minecraft server on my website. But I can't get it to work.

function achievements() {
    global $id;
    $sql="SELECT * FROM achievements
           INNER JOIN stats ON achievements.type=stats.type
           INNER JOIN stats ON achievements.block=stats.block
           INNER JOIN stats ON achievements.data=stats.data
           INNER JOIN stats ON achievements.value=stats.value
           WHERE player_id = $id";
    $result=mysql_query($sql) or die(mysql_error());
    $rows=mysql_fetch_array($result);
}   

我可以使用 $rows['achievements.type'];$rows['stats.type']; 来获取列type"吗?" 来自所选表格,还是有其他方法可以做到?

Will I be able to use $rows['achievements.type']; and $rows['stats.type']; to get the column "type" from the selected table, or is there a another way to do it?

列名和表名是我用的插件定义的,所以不能改名.

The column and table names are defined by the plugin I use, so the names can't be changed.

推荐答案

它不工作的原因是因为(在我自己看来)服务器对它将如何运行有点困惑正确处理列名.为了使其工作,在每个具有相同名称的表以及要加入的列上添加别名,例如

the reason why it is not working is because (in my own opinion) the server is a little confused on where how it will handle the columns names properly. In order it to work, add an alias on every table that has the same name that you want to join as well as the columns, eg

SELECT  achievements.*,
        a.Name as TypeName,
        b.Name AS BlockName,
        c.Name as DataName,
        d.Name AS ValueName
FROM    achievements
        INNER JOIN stats a ON achievements.type = a.type
        INNER JOIN stats b ON achievements.block = b.block
        INNER JOIN stats c ON achievements.data = c.data
        INNER JOIN stats d ON achievements.value = d.value
WHERE   player_id = $id

假设您想获取每个特定列的名称.

这篇关于内连接 2 个具有相同列名的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆