MySQL - 选择并加入和重命名列

时间:2016-05-25 20:15:47

标签: mysql

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

master_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>
&#13;
&#13;
&#13;

master_biz.asset_location表

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

master_custom.custom_app_table_4表

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

我目前正在使用以下代码加入其中两个表格。 这段代码可行,但是我留下了许多额外的列和列名,这些列和列名并不能准确地表示数据。

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 由于主要问题已得到解决,我觉得提出另一个问题是为了让未来帮助寻求者不那么忙。

2 个答案:

答案 0 :(得分:1)

您重新加载同一张桌子两次:<​​/ p>

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

答案 1 :(得分:1)

看起来你加入了&#34; master_biz.legend_asset&#34;表两次。您需要为每个提供别名才能使其工作(请参阅下面的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