我试了几个小时并阅读了很多帖子,但我仍然无法弄清楚如何处理这个请求:
我有一张这样的表:
+------+------+
|ARIDNR|LIEFNR|
+------+------+
|1 |A |
+------+------+
|2 |A |
+------+------+
|3 |A |
+------+------+
|1 |B |
+------+------+
|2 |B |
+------+------+
我想选择使用不同LIEFNR多次出现的ARIDNR。
输出应该是这样的:
+------+------+
|ARIDNR|LIEFNR|
+------+------+
|1 |A |
+------+------+
|1 |B |
+------+------+
|2 |A |
+------+------+
|2 |B |
+------+------+
答案 0 :(得分:47)
应该这样做:
SELECT *
FROM YourTable
WHERE ARIDNR IN (
SELECT ARIDNR
FROM YourTable
GROUP BY ARIDNR
HAVING COUNT(*) > 1
)
我们的想法是使用内部查询来识别在数据中出现1次以上ARIDNR
值的记录,然后根据该组值从同一个表中获取所有列。
答案 1 :(得分:32)
请试试这个。我检查了它并且它正在工作:
SELECT *
FROM Table
WHERE ARIDNR IN (
SELECT ARIDNR
FROM Table
GROUP BY ARIDNR
HAVING COUNT(distinct LIEFNR) > 1
)
答案 2 :(得分:11)
加入同一张桌子。使用内部联接,以便丢弃不匹配的行。在连接集中,将在表中的另一行中具有匹配的ARIDNR的行具有不同的LIEFNR。允许这些ARIDNR出现在最终集合中。
SELECT * FROM YourTable WHERE ARIDNR IN (
SELECT a.ARIDNR FROM YourTable a
JOIN YourTable b on b.ARIDNR = a.ARIDNR AND b.LIEFNR <> a.LIEFNR
)
答案 3 :(得分:1)
这是一个旧问题但我发现我还需要不时提供解决方案。以前的答案都很好,效果很好,我个人更喜欢使用CTE,例如:
<SheetData>
<Finance>
<value>1000</value>
<Currency>USD</Currency>
<Attribute>Sales</Attribute>
</Finance>
<Finance>
<value>5000</value>
<Currency>USD</Currency>
<Attribute>Assets</Attribute>
</Finance>
</SheetData>
产生以下结果:
1 | A
1 |乙
2 |乙
2 | A
答案 4 :(得分:0)
你可以通过
简单地实现它SELECT *
FROM test
WHERE ARIDNR IN
(SELECT ARIDNR FROM test
GROUP BY ARIDNR
HAVING COUNT(*) > 1)
GROUP BY ARIDNR, LIEFNR;
感谢。
答案 5 :(得分:0)
使用此
select * from (
SELECT ARIDNR,LIEFNR,row_number() over
(partition by ARIDNR order by ARIDNR) as RowNum) a
where a.RowNum >1
答案 6 :(得分:0)
Select A.ARIDNR,A.LIEFNR
from Table A
join Table B
on A.ARIDNR = B.ARIDNR
and A.LIEFNR<> B.LIEFNR
group by A.ARIDNR,A.LIEFNR
答案 7 :(得分:-1)
$sql="SELECT * FROM TABLE_NAME WHERE item_id=".$item_id;
$query=mysql_query($sql);
while($myrow=mysql_fetch_array($query)) {
echo print_r($myrow,1);
}