获取第二个重复列的SQL查询结果。
P粉037880905
2023-08-02 12:42:13
<p>我在运行SQL查询时遇到了一个挑战:<br /><br />有两个表:</p><p><br /></p>
<ul>
<li>Sample Table 01</li>
</ul>
<table class="s-table">
<thead>
<tr>
<th>Ext. Initial ID</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>1111</td>
<td>start11</td>
</tr>
<tr>
<td>1112</td>
<td>start12</td>
</tr>
<tr>
<td>1113</td>
<td>start13</td>
</tr>
<tr>
<td>1114</td>
<td>start14</td>
</tr>
<tr>
<td>1115</td>
<td>start15</td>
</tr>
<tr>
<td>1116</td>
<td>start16</td>
</tr>
<tr>
<td>1117</td>
<td>start17</td>
</tr>
<tr>
<td>1118</td>
<td>start18</td>
</tr>
<tr>
<td>1119</td>
<td>start19</td>
</tr>
<tr>
<td>1120</td>
<td>start20</td>
</tr>
</tbody>
</table>
<ul>
<li>Sample Table 02</li>
</ul>
<table class="s-table">
<thead>
<tr>
<th>Ext. Initial ID</th>
<th>Ext. Level one ID</th>
</tr>
</thead>
<tbody>
<tr>
<td>1111</td>
<td>1115</td>
</tr>
<tr>
<td>1112</td>
<td>1115</td>
</tr>
<tr>
<td>1113</td>
<td>1113</td>
</tr>
<tr>
<td>1114</td>
<td>1113</td>
</tr>
<tr>
<td>1115</td>
<td>1113</td>
</tr>
<tr>
<td>1116</td>
<td>1113</td>
</tr>
<tr>
<td>1117</td>
<td>1119</td>
</tr>
<tr>
<td>1118</td>
<td>1119</td>
</tr>
<tr>
<td>1119</td>
<td>1119</td>
</tr>
<tr>
<td>1120</td>
<td>1119</td>
</tr>
</tbody>
</table>
<p>"Ext. Initial ID"和"Description"列是唯一的。<br /><br />而"Ext. Level one ID"是重复的(但它基本上是"Ext. Initial ID"的一部分)。</p><p><br /></p>
<pre class="brush:php;toolbar:false;">**Query Im using:**
SELECT
I.external_initial_id,
C.external_level_one_id,
I.description
FROM sample_table_01 AS I
inner join sample_table_02 AS C
ON I.external_initial_id = C.external_initial_id</pre>
<p>现在,我只需要一些"Ext. Initial ID"的结果,所以我使用一个过滤器(以逗号分隔的方式在仪表板中)将它们放入查询中,查询结果如下:</p>
<pre class="brush:php;toolbar:false;">
| Ext. Initial ID | Ext. Level one ID | Description |
| -------- | -------- | -------- |
| 1111 | 1115 | start11 |
| 1112 | 1115 | start12 |
| 1113 | 1113 | start13 |
| 1114 | 1113 | start14 |
| 1117 | 1119 | start17 |
| 1119 | 1119 | start19 |</pre>
<p>在这里,我得到了与表01中的"Ext. Initial ID"相关联的"description"列。<br /><br />然而,我想要的是与"Ext. Level one ID"相关联的"description"列(即来自另一个表的结果列)。<br /><br />我期望的输出是:</p><p><br /></p>
<table class="s-table">
<thead>
<tr>
<th>Ext. Initial ID</th>
<th>Ext. Level one ID</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>1111</td>
<td>1115</td>
<td>start15</td>
</tr>
<tr>
<td>1112</td>
<td>1115</td>
<td>start15</td>
</tr>
<tr>
<td>1113</td>
<td>1113</td>
<td>start13</td>
</tr>
<tr>
<td>1114</td>
<td>1113</td>
<td>start13</td>
</tr>
<tr>
<td>1117</td>
<td>1119</td>
<td>start19</td>
</tr>
<tr>
<td>1119</td>
<td>1119</td>
<td>start19</td>
</tr>
</tbody>
</table><p><br /></p>
你可以尝试使用以下的查询语句:
SELECT C.external_initial_id,C.external_level_one_id,I.description FROM sample_table_02 AS C inner join sample_table_01 AS I ON C.external_level_one_id = I.external_initial_id