当从 MySQL 选择所有内容时,我可以重命名特定字段,但仍然使用 * 选择其他所有内容

发布于 2024-11-09 02:14:48 字数 858 浏览 8 评论 0原文

Hey again guys. So, I'm trying to find out how to select every field across multiple tables in a mysql database and output the resulting table to a .csv file for Excel.我发现了处理 .csv 输出的臭名昭著的 stackoverflow 问题,并且那里有所有代码。 So, I guess what I'm asking is:

Is there a way to do a mysql query that looks something like this?

SELECT * prof.id AS youth_id FROM time time, profiles prof, WHERE foo='bar'

and have it select everything from both (or like 4) tables but select the id as youth_id, or do I have to specify EVERY value that I want if I need to select certain values as another name?

I would like to not have to specify every field as I have like 50+ to output.

I've tried to search but can't really find what I need. I guess if you could point me in the right direction that would be great.

Hey again guys. So, I'm trying to find out how to select every field across multiple tables in a mysql database and output the resulting table to a .csv file for Excel. I've found the infamous stackoverflow question dealing with .csv ouput and have all the code there. So, I guess what I'm asking is:

Is there a way to do a mysql query that looks something like this?

SELECT * prof.id AS youth_id FROM time time, profiles prof, WHERE foo='bar'

and have it select everything from both (or like 4) tables but select the id as youth_id, or do I have to specify EVERY value that I want if I need to select certain values as another name?

I would like to not have to specify every field as I have like 50+ to output.

I've tried to search but can't really find what I need. I guess if you could point me in the right direction that would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔戏命师 2024-11-16 02:14:49

您不必指定每个字段。相反,您可以执行以下操作:

SELECT *, prof.id AS youth_id FROM time time, profiles prof

但是,上面的操作将返回两个表中的所有列以及您重命名的列。因此,如果原始两个表总共有 4 列,并且您重命名其中一列,那么您将在结果集中获得 5 列。

You don't have to specify every field. Instead you can do the following:

SELECT *, prof.id AS youth_id FROM time time, profiles prof

However the above will return all columns from the two tables plus the column you renamed. So if the original two tables have 4 columns total and you rename one, you will get 5 columns in the result set.

寂寞清仓 2024-11-16 02:14:49

瑞安,你可以做你想做的事。唯一的问题是,结果将包括 Youth_id 和 id,且两者的值相同。如果你只想要 Youth_id &不是 id,那么您必须列出所需的所有列,而不是使用“*”。顺便说一下,如果你有 phpmyadmin,你可以直接输入 sql 并立即看到结果。

Ryan, you can do what you want to. The only catch is, the results will include both youth_id and id with the same values for each. If you want only youth_id & not id, then you'll have to list all the columns you want instead of using '*'. Incidentally, if you have phpmyadmin handy, you could just enter the sql and see the result immediately.

小梨窩很甜 2024-11-16 02:14:49

好吧,因为似乎没有其他解决方案是完美的,所以我要跳出框框思考一下。如果你不介意打两个电话的话。一种可能是表模式(存储列名的位置),然后您可以根据需要以编程方式更改名称(无需全部键入)。之后,使用内爆数组作为其 SELECT 部分进行第二次调用。

一个例子:(

<?php
$tables = array ('table1','table2','table3');
$NameAliases = array('table1.id'=>'profid');

foreach ($tables as $table)
{
    $sql = "SHOW COLUMNS FROM $table;";
    $result = mysql_query($sql);

    $columns = array();

    while ($row = mysql_fetch_array($result))
    {
        $search = array_search($table.'.'.$row["Field"],$NameAliases);
        if ($search === true)
        {
            $parts = explode('.',$NameAliases[$search]);
            $columns[] = "$table.{$row["Field"]} AS {$parts[1]}"; //get the side of the dot
        }
        else
        {
            $columns[] = "$table.{$row["Field"]}";
        }
    }
}
$columnstring = implode(', ',$columns);
$sql = "SELECT $columnstring FROM ".implode(', ',$tables)." WHERE 1=1";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result))
{

}
?>

不知道上述是否有效)

Well, because no other solution seems perfect, I'm going to think outside the box for a sec. If you don't mind making two calls. One could be to the table schema (where the column names are stored), and then you could programmically change names as much as needed (without typing them all). After that, make a second call with the imploded array as its SELECT part.

An example:

<?php
$tables = array ('table1','table2','table3');
$NameAliases = array('table1.id'=>'profid');

foreach ($tables as $table)
{
    $sql = "SHOW COLUMNS FROM $table;";
    $result = mysql_query($sql);

    $columns = array();

    while ($row = mysql_fetch_array($result))
    {
        $search = array_search($table.'.'.$row["Field"],$NameAliases);
        if ($search === true)
        {
            $parts = explode('.',$NameAliases[$search]);
            $columns[] = "$table.{$row["Field"]} AS {$parts[1]}"; //get the side of the dot
        }
        else
        {
            $columns[] = "$table.{$row["Field"]}";
        }
    }
}
$columnstring = implode(', ',$columns);
$sql = "SELECT $columnstring FROM ".implode(', ',$tables)." WHERE 1=1";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result))
{

}
?>

(No clue if the above works at all)

饮湿 2024-11-16 02:14:49

如果您确实不希望从 a 中重复该列(即同时获取 id 和 Youth_id),

SELECT *, prof.id AS youth_id 
FROM time 
INNER JOIN profiles prof 
   ON time.profile_id = prof.id 
WHERE foo = 'bar'

您可以从每个表中 SELECT * ,除了您想要别名的列的表之外:

SELECT time.*, prof.id AS youth_id, prof.forename, prof.surname 
FROM time 
INNER JOIN profiles prof 
   ON time.profile_id = prof.id 
WHERE foo = 'bar'

这将为您提供所有信息来自时间的列,以及来自配置文件的枚举列。您仍然需要枚举表中要为列添加别名的所有列,但至少不必列出每一列。

当然,还有一种观点认为,无论如何您都应该枚举所有列,以防您的架构在某个时候发生变化,但那是另一个故事了。

If you really don't want that column duplication (i.e. getting both id and youth_id) from a

SELECT *, prof.id AS youth_id 
FROM time 
INNER JOIN profiles prof 
   ON time.profile_id = prof.id 
WHERE foo = 'bar'

You can SELECT * from each table except the table with the column you wish to alias:

SELECT time.*, prof.id AS youth_id, prof.forename, prof.surname 
FROM time 
INNER JOIN profiles prof 
   ON time.profile_id = prof.id 
WHERE foo = 'bar'

This will get you all columns from time, and the enumerated ones from profiles. You still have to enumerate all columns in the table you're looking to alias a column in, but at least you don't have to list every single one.

Of course, there's also the argument that you should enumerate all columns anyway in case your schema changes at some point, but that's another story.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文