【问题标题】:MySQL - Select and Join and Rename ColumnsMySQL - 选择、加入和重命名列
【发布时间】:2016-05-25 20:15:47
【问题描述】:

我有三个表,其中两个在 master_biz 数据库中,第三个在 master_custom 数据库中。

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>

我目前正在使用以下代码来连接其中两个表。 此代码有效,但我留下了许多无法准确表示数据的额外列和列名。

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

我想使用id 列和iid 列连接所有这三个表。我还需要能够仅选择我需要的列。

我认为应该怎么做:

SELECT
 master_custom.custom_app_table_4.ID AS ASSET_ID,
 master_custom.custom_app_table_4.2_2 AS CONTRACT_TYPE,
 master_custom.custom_app_table_4.8_2 AS SUPPLIER,
 master_custom.custom_app_table_4.9_2 AS SUPPORT_LEVEL,
 master_custom.custom_app_table_4.10_2 AS QUANTITY,
 master_custom.custom_app_table_4.11_2 AS LICENSE_EXPIRATION,
 master_custom.custom_app_table_4.12_2 AS LICENSE_COST,
 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
FROM
 master_custom.custom_app_table_4,
 master_biz.legend_asset
Left JOIN
 master_biz.legend_asset
ON
 master_custom.custom_app_table_4.ASSET_ID=master_biz.legend_asset.ID

执行时收到错误代码:

#1066 - Not unique table/alias: 'legend_asset'

如果有人知道如何实现这一点并愿意分享,将不胜感激。

我原来的帖子在这里: MySQL and Splunk - Select and Join 既然主要问题已经解决,我觉得创建另一个问题来让未来的求助者不那么忙是合适的。

【问题讨论】:

  • 我看到我提出的这样做的方法是如何失败的。我仍然不熟悉实现目标的方法。 “我想使用 id 列和 iidcolumn 连接所有这三个表。我还需要能够仅选择我需要的列。”

标签: mysql


【解决方案1】:

您似乎加入了“master_biz.legend_asset”表两次。您需要为每个人提供一个别名才能完成这项工作(请参阅下面的 mbla1 和 mbla2)。您还需要为 Select 子句中的每一列使用这些别名。

SELECT
 master_custom.custom_app_table_4.ID AS ASSET_ID,
 master_custom.custom_app_table_4.2_2 AS CONTRACT_TYPE,
 master_custom.custom_app_table_4.8_2 AS SUPPLIER,
 master_custom.custom_app_table_4.9_2 AS SUPPORT_LEVEL,
 master_custom.custom_app_table_4.10_2 AS QUANTITY,
 master_custom.custom_app_table_4.11_2 AS LICENSE_EXPIRATION,
 master_custom.custom_app_table_4.12_2 AS LICENSE_COST,
 mbla1.roa_id AS ORGANIZATION_NUMBER,   --Aliased column
 mbla1.make AS MANUFACTURER,            --Aliased column
 mbla2.model AS PRODUCT,                --Aliased column
 mbla2.status AS STATUS                  --Aliased column
FROM
 master_custom.custom_app_table_4,
 master_biz.legend_asset mbla1          --Aliased table
Left JOIN
 master_biz.legend_asset mbla2          --Aliased table
ON
 master_custom.custom_app_table_4.ASSET_ID=master_biz.legend_asset.ID

【讨论】:

  • 我试过这个没有用。我更正了丢失的“。”在mbla2status AS STATUS 。当我执行查询时,我收到以下错误:#1054 - Unknown column ` ` master_custom.custom_app_table_4.ASSET_ID' in on clause
  • 将 ` ` master_custom.custom_app_table_4.ASSET_ID=master_biz.legend_asset.ID 更改为 ` ` ` master_custom.custom_app_table_4.ID=master_biz.legend_asset.ID 会导致相同的 #1054 - Unknown column 'master_custom.custom_app_table_4.ID' in 'on clause' 错误。
  • 对不起 mbla2.status 的错误;我已经纠正了它。关于“on”子句中的问题,您可以尝试在“From”子句中为“master_custom.custom_app_table_4”表起别名,并在“On”子句中使用相同的别名(而不是表名)。跨度>
  • 我不担心错误。我只是指出以防有人稍后回来尝试使用此答案。通过一些调整,我能够让它工作。我想我只是休息了一天,因为我进来重读了所有内容,它点击了。我最终使用的代码是:
  • 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_custom.custom_app_table_4.ID AS ASSET_ID, master_custom .custom_app_table_4.2_2 AS CONTRACT_TYPE, master_custom.custom_app_table_4.8_2 AS SUPPLIER, master_custom.custom_app_table_4.9_2 AS SUPPORT_LEVEL FROM master_biz.legend_asset LEFT JOIN master_custom.custom_app_table_4 USING (id);
【解决方案2】:

您正在加载同一个表两次:

FROM
 master_custom.custom_app_table_4,
 master_biz.legend_asset    <---#1
Left JOIN
 master_biz.legend_asset    <---#2

表的这两次提及中至少有一个必须被别名为一个唯一的名称,例如:

FROM
 master_custom.custom_app_table_4,
 master_biz.legend_asset   AS foo
Left JOIN
 master_biz.legend_asset   AS bar

然后在查询中的其他任何地方酌情使用foobar

【讨论】:

    猜你喜欢
    • 2019-12-16
    • 2019-08-28
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    相关资源
    最近更新 更多