gpt4 book ai didi

MySQL 和 Splunk - 选择并加入

转载 作者:行者123 更新时间:2023-11-28 23:30:35 26 4
gpt4 key购买 nike

在 splunk 中设置 DBconnect 查询时,我遇到以下代码问题。

SELECT * FROM master_biz.legend_asset
RIGHT JOIN
master_custom.custom_app_table_4
ON
master_custom.custom_app_table_4.ID = master_biz.legend_asset.ID

当我使用上面的代码时,它在 PHPmyAdmin 中完美执行。但是,当我尝试在 Splunk 中使用它时,我收到一条错误消息:

Invalid Query
External search command 'dbxquery' returned error code 1. Script output =
"RuntimeError: Failed to run query: "SELECT * FROM (SELECT * FROM
master_biz.legend_asset RIGHT JOIN master_custom.custom_app_table_4 ON
master_custom.custom_app_table_4.ID = master_biz.legend_asset.ID) t", caused
by:AvroRemoteException(u"com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : Duplicate column name 'id'",).

此错误表明我有重复的名为“ID”的列。我认为这是清理一些数据的最佳时机,因此我尝试重命名 ID 字段,如下所示:

SELECT 
master_biz.legend_asset.roa_id AS ORGANIZATION_NUMBER,
master_biz.legend_asset.make AS MANUFACTURER,
master_biz.legend_asset.model AS PRODUCT,
master_biz.legend_asset.status AS STATUS,
master_biz.legend_asset.ID AS ASSET_ID
FROM master_biz.legend_asset
RIGHT JOIN master_custom.custom_app_table_4 ON
master_custom.custom_app_table_4.ID = master_biz.legend_asset.ID

但是,当我尝试此查询时,只剩下我重命名的字段,而没有来自 custom_app_table_4 的字段。考虑到这可能是由于 ID 字段的重命名,我将查询更改为:

SELECT 
master_biz.legend_asset.roa_id AS ORGANIZATION_NUMBER,
master_biz.legend_asset.make AS MANUFACTURER,
master_biz.legend_asset.model AS PRODUCT,
master_biz.legend_asset.status AS STATUS,
master_biz.legend_asset.ID AS ASSET_ID
FROM master_biz.legend_asset
RIGHT JOIN master_custom.custom_app_table_4 ON
master_custom.custom_app_table_4.ID = master_biz.legend_asset.ASSET_ID

这导致了以下错误:

#1054 - Unknown column master_biz.legend_asset.ASSET_ID in on clause

ma​​ster_biz.legend_asset 表

<style type="text/css">
table.tableizer-table {
font-size: 12px;
border: 1px solid #CCC;
font-family: Arial, Helvetica, sans-serif;
}
.tableizer-table td {
padding: 4px;
margin: 3px;
border: 1px solid #CCC;
}
.tableizer-table th {
background-color: #104E8B;
color: #FFF;
font-weight: bold;
}
</style>
<table class="tableizer-table">
<thead>
<tr class="tableizer-firstrow">
<th>id</th>
<th>did</th>
<th>roa_id</th>
<th>make</th>
<th>model</th>
<th>type</th>
<th>function</th>
<th>status</th>
<th>owner</th>
<th>serial</th>
<th>asset_tag</th>
<th>rfid</th>
<th>date_edit</th>
<th>user_edit</th>
<th>a_notes</th>
<th>owner_admin</th>
<th>owner_tech</th>
</tr>
</thead>
<tbody>
<tr>
<td>2</td>
<td>0</td>
<td>1</td>
<td>Tenable</td>
<td>Nessus</td>
<td>&nbsp;</td>
<td>Unknown</td>
<td>Production</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>NULL</td>
<td>&nbsp;</td>
<td>5/23/2016 16:19</td>
<td>1</td>
<td>&nbsp;</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>3</td>
<td>0</td>
<td>1</td>
<td>Tenable</td>
<td>Nessus</td>
<td>&nbsp;</td>
<td>Unknown</td>
<td>Production</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>NULL</td>
<td>&nbsp;</td>
<td>5/20/2016 18:59</td>
<td>1</td>
<td>&nbsp;</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>4</td>
<td>0</td>
<td>2</td>
<td>Microsoft</td>
<td>Windows Server Standard 2012 R2</td>
<td>&nbsp;</td>
<td>Unknown</td>
<td>Production</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>NULL</td>
<td>&nbsp;</td>
<td>5/20/2016 18:59</td>
<td>1</td>
<td>&nbsp;</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>5</td>
<td>0</td>
<td>0</td>
<td>Solarwinds</td>
<td>Kiwi CAT Tools</td>
<td>&nbsp;</td>
<td>Unknown</td>
<td>Production</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>NULL</td>
<td>&nbsp;</td>
<td>5/20/2016 18:59</td>
<td>1</td>
<td>&nbsp;</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>6</td>
<td>0</td>
<td>1</td>
<td>Splunk</td>
<td>Enterprise</td>
<td>&nbsp;</td>
<td>Unknown</td>
<td>Production</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>NULL</td>
<td>&nbsp;</td>
<td>5/20/2016 18:59</td>
<td>1</td>
<td>&nbsp;</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>7</td>
<td>0</td>
<td>1</td>
<td>Splunk</td>
<td>Enterprise Support</td>
<td>&nbsp;</td>
<td>Unknown</td>
<td>Production</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>NULL</td>
<td>&nbsp;</td>
<td>5/23/2016 16:19</td>
<td>1</td>
<td>&nbsp;</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>8</td>
<td>0</td>
<td>1</td>
<td>VMware</td>
<td>vSphere 5/6 Support Standard</td>
<td>&nbsp;</td>
<td>Unknown</td>
<td>Production</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>NULL</td>
<td>&nbsp;</td>
<td>5/20/2016 18:59</td>
<td>1</td>
<td>&nbsp;</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>9</td>
<td>0</td>
<td>1</td>
<td>VMware</td>
<td>vSphere 5/6 Support Enterprise Plus</td>
<td>&nbsp;</td>
<td>Unknown</td>
<td>Production</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>NULL</td>
<td>&nbsp;</td>
<td>5/20/2016 18:59</td>
<td>1</td>
<td>&nbsp;</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>10</td>
<td>0</td>
<td>1</td>
<td>VMware</td>
<td>vCenter 5/6 Support Standard</td>
<td>&nbsp;</td>
<td>Unknown</td>
<td>Production</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>NULL</td>
<td>&nbsp;</td>
<td>5/20/2016 18:59</td>
<td>1</td>
<td>&nbsp;</td>
<td>0</td>
<td>0</td>
</tr>
</tbody>
</table>

ma​​ster_biz.asset_location 表

<style type="text/css">
table.tableizer-table {
font-size: 12px;
border: 1px solid #CCC;
font-family: Arial, Helvetica, sans-serif;
}
.tableizer-table td {
padding: 4px;
margin: 3px;
border: 1px solid #CCC;
}
.tableizer-table th {
background-color: #104E8B;
color: #FFF;
font-weight: bold;
}
</style>
<table class="tableizer-table">
<thead>
<tr class="tableizer-firstrow">
<th>iid</th>
<th>location</th>
<th>floor</th>
<th>room</th>
<th>plate</th>
<th>panel</th>
<th>punch</th>
<th>zone</th>
<th>rack</th>
<th>shelf</th>
<th>date_edit</th>
<th>user_edit</th>
<th>l_notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>&nbsp;</td>
<td>0</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>0000-00-00 00:00:00</td>
<td>1</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>2</td>
<td>Lab</td>
<td>0</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>0000-00-00 00:00:00</td>
<td>1</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>3</td>
<td>Production</td>
<td>0</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>0000-00-00 00:00:00</td>
<td>1</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>4</td>
<td>&nbsp;</td>
<td>0</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>0000-00-00 00:00:00</td>
<td>1</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>5</td>
<td>&nbsp;</td>
<td>0</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>0000-00-00 00:00:00</td>
<td>1</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>6</td>
<td>&nbsp;</td>
<td>0</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>0000-00-00 00:00:00</td>
<td>1</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>7</td>
<td>&nbsp;</td>
<td>0</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>0000-00-00 00:00:00</td>
<td>1</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>8</td>
<td>&nbsp;</td>
<td>0</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>0000-00-00 00:00:00</td>
<td>1</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>9</td>
<td>&nbsp;</td>
<td>0</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>0000-00-00 00:00:00</td>
<td>1</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>10</td>
<td>&nbsp;</td>
<td>0</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>0000-00-00 00:00:00</td>
<td>1</td>
<td></td>
</tr>
</tbody>
</table>

ma​​ster_custom.custom_app_table_4 表

<style type="text/css">
table.tableizer-table {
font-size: 12px;
border: 1px solid #CCC;
font-family: Arial, Helvetica, sans-serif;
}
.tableizer-table td {
padding: 4px;
margin: 3px;
border: 1px solid #CCC;
}
.tableizer-table th {
background-color: #104E8B;
color: #FFF;
font-weight: bold;
}
</style>
<table class="tableizer-table">
<thead>
<tr class="tableizer-firstrow">
<th>id</th>
<th>2_2</th>
<th>8_2</th>
<th>9_2</th>
<th>10_2</th>
<th>11_2</th>
<th>12_2</th>
<th>13_2</th>
</tr>
</thead>
<tbody>
<tr>
<td>2</td>
<td>Software License</td>
<td>Tenable</td>
<td>Professional</td>
<td>1</td>
<td>5/10/2017</td>
<td>2190</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>3</td>
<td>Software License</td>
<td>Tenable</td>
<td>Professional</td>
<td>1</td>
<td>5/10/2017</td>
<td>2190</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>4</td>
<td>Software License</td>
<td>Microsoft</td>
<td>Standard</td>
<td>10</td>
<td>5/3/2016</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>5</td>
<td>Software Maintenance</td>
<td>Solarwinds</td>
<td>N/A</td>
<td>4</td>
<td>10/30/2016</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>6</td>
<td>Software License</td>
<td>Splunk</td>
<td>20GB</td>
<td>1</td>
<td>6/1/2016</td>
<td>60000</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>7</td>
<td>Software Maintenance</td>
<td>Splunk</td>
<td>Enterprise</td>
<td>1</td>
<td>6/1/2016</td>
<td>0</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>8</td>
<td>Software Maintenance</td>
<td>VMware</td>
<td>24x7 Production</td>
<td>30</td>
<td>5/10/2017</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>9</td>
<td>Software Maintenance</td>
<td>VMware</td>
<td>Subscription Only</td>
<td>46</td>
<td>5/10/2017</td>
<td>4375</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>10</td>
<td>Software Maintenance</td>
<td>VMware</td>
<td>Subscription Only</td>
<td>3</td>
<td>5/10/2017</td>
<td>530</td>
<td></td>
</tr>
</tbody>
</table>

最终,我试图通过一个问题来克服几个障碍。我想从多个数据库中的不同表中加入选择列,并将这些列重命名为更合乎逻辑的名称。

简而言之,我希望能够使用 SQL 查询将这三个表合并为一个表。如果可能,最好更改一些字段的名称,以便数据更易于解释。如果能够只从每个表中选择我想要的字段,那就太棒了。

通过组合这些表并仅选择感兴趣的字段,它将节省 splunk 中的日常使用许可证、磁盘存储空间以及搜索期间的 cpu 使用率。

非常感谢任何帮助。

最佳答案

SELECT * 是反模式。如果 id 只是两个表中都存在的列,您可以使用:

SELECT *
FROM master_biz.legend_asset
RIGHT JOIN master_custom.custom_app_table_4
USING (id);

否则你需要手动为每一列添加别名:

SELECT a.ID    AS id
,a. ... AS ...
,t4.col AS ...
FROM master_biz.legend_asset a
RIGHT JOIN master_custom.custom_app_table_4 t4
ON a.ID = t4.ID;

注意:表名不用写,可以用表别名。

编辑:

what are the differences in the JOIN ON and JOIN USING parts of the code?

USING 将返回在 JOIN 中使用一次的列:

SELECT *
FROM t1
JOIN t2
USING(i);

SELECT *
FROM t1
JOIN t2
ON t1.i = t2.i;

SqlFiddleDemo

输出:

╔════╦════╦═══╗
║ i ║ b ║ c ║
╠════╬════╬═══╣
║ 1 ║ 1 ║ 3 ║
╚════╩════╩═══╝

对比

╔════╦════╦════╦═══╗
║ i ║ b ║ i ║ c ║
╠════╬════╬════╬═══╣
║ 1 ║ 1 ║ 1 ║ 3 ║
╚════╩════╩════╩═══╝

关于MySQL 和 Splunk - 选择并加入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37445169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com