开发者

MySQL return unique columname with 2 joins of equal table

开发者 https://www.devze.com 2023-02-05 04:15 出处:网络
I have the following query: SELECT * FROM scool LEFT JOIN people as t1 ON t1.scool_fk=scool.id AND t1.lang_gk=\'en\'

I have the following query:

SELECT * 
FROM scool
LEFT JOIN people as t1 ON t1.scool_fk=scool.id AND t1.lang_gk='en'
LEFT JOIN people as t2 ON t2.scool_fk=scool.id AND t2.lang_gk='fr'

... (this is nonsense query, only for example)

With SELECT * it query returns french values

With SELECT t1.* it q开发者_如何学编程uery returns english values

The only possible solution i know is

SELECT t1.name as name_en, t2.name as name_fr

This don't like me because i can't automatize the selects in my program

Is possible to be return all values from SELECT with tablename.columnname or other similar solution, for get values in php?


Use mysql_fetch_field and properties table and name:

<?php
        mysql_connect("", "", "");
        mysql_select_db("test");
        $res = mysql_query("SELECT * FROM t_ai a1 CROSS JOIN t_ai a2 LIMIT 1") or die(mysql_error());
        $names = array();
        for ($i = 0; $i < mysql_num_fields($res); $i++) {
                $meta = mysql_fetch_field($res, $i);
                array_push($names, "$meta->table.$meta->name");
        }
        print implode($names, "\t") . "\n";
?>

$ php phptest.php
a1.id   a2.id
0

精彩评论

暂无评论...
验证码 换一张
取 消