从一列中选择其他库存中具有相同值的所有不同记录

时间:2016-09-21 08:22:10

标签: sql sqlite visual-foxpro

我可以使用一些帮助善良的人!

我在一个名为x:

的表中有三列
ID | Barcode| Description  
1  | 554412 | Bar  
2  | 554412 | Stone  
3  | 554412 | Bar  

我需要的是选择所有条形码和ID,其中相同的条形码具有不同的描述,因此在这种情况下,它将向我显示带有条形和石头描述的ID 1和2。

非常感谢你!

7 个答案:

答案 0 :(得分:1)

由于它被标记为SQL和VFP,我将在该上下文中回答思考。以下是您的表格的一些扩展示例数据:

Create Cursor myTable (Id I, Barcode c(10), Description c(10))

Insert Into myTable (Id, Barcode, Description) Values (1,'554412', 'Bar')
Insert Into myTable (Id, Barcode, Description) Values (2,'554412', 'Stone')
Insert Into myTable (Id, Barcode, Description) Values (3,'554412', 'Bar')

Insert Into myTable (Id, Barcode, Description) Values (4,'554413', 'Bar1')
Insert Into myTable (Id, Barcode, Description) Values (5,'554413', 'Bar2')
Insert Into myTable (Id, Barcode, Description) Values (6,'554413', 'Bar3')
Insert Into myTable (Id, Barcode, Description) Values (7,'554413', 'Bar3')
Insert Into myTable (Id, Barcode, Description) Values (8,'554413', 'Bar1')

使用SQL,很容易得到你所显示的内容:

Select * From myTable ;
    where Id In ;
    (Select Min(Id) From myTable Group By Barcode, Description)

仅仅为了VFP,另一个不需要其他任何东西的简单技巧就是创建一个独特的索引:

Index on Barcode + Description tag tmp unique
* Browse

答案 1 :(得分:0)

这表示条形码具有多个不同的描述,以及相关的ID

select x.id, b.Barcode, b.Duplicates
from
(
select Barcode, count(distinct Description) as Duplicates
from x
group by Barcode
having count(distinct Description) >1
) b
inner join x
on x.Barcode = b.Barcode

答案 2 :(得分:0)

Select x1.ID , x1.Barcode from x as x1 , x as x2 where 
x1.Barcode = x2.Barcode and x2.Description != x1.Description

这将返回示例中的第1,2和3行,因为它们都符合&#34的标准;相同的条形码具有不同的描述。"

将返回第1行和第3行,因为它们的条形码和描述与第2行相同。

答案 3 :(得分:0)

请检查以下答案。

<?php
    $foo = 'foo';
    $nl = htmlspecialchars('\n');
    $bar = 'bar';

    echo $foo . $nl . $bar;
?>

答案 4 :(得分:0)

当你说 你需要的是选择所有条形码和ID,其中相同的条形码有不同的描述时,这个问题的答案可能很棘手, 然后结果集也可以是id 2和3。所以我已经采用了最少的id代表第一场比赛。见下文:

with tabl (ID , Barcode, Description)
          as ( select 1 , 554412 ,'Bar' from dual
               UNION ALL
                select 2 ,554412 ,'Stone' from dual
               UNION ALL
                select 3 ,554412 ,'Bar' from dual )
select min(a.id) id,a.barcode,a.description 
from tabl a
inner join  tabl b
on a.Barcode = b.Barcode
group by a.barcode,a.description;

答案 5 :(得分:0)

请你试试这个,希望它能满足你的要求:

CREATE TABLE tmpTwo(ID INT, Barcode INT, DescriptionE VARCHAR(50))
INSERT INTO tmpTwo VALUES 
(1,554412, 'Bar'),
(2,554412, 'Stone'),
(3,554412, 'Bar'),
(4,554413, 'b'),
(5,554413, 's'),
(6,554413, 'b'),
(7,554414, 'z')

SELECT s.id, s.DescriptionE
FROM tmpTwo s
INNER JOIN(SELECT MAX(u.id) it FROM tmpTwo u
       WHERE u.barcode IN (SELECT v.barcode FROM tmpTwo v 
                         GROUP BY v.barcode 
                         HAVING (COUNT(DISTINCT(v.descriptionE)))>1 )
GROUP BY u.descriptionE) t ON s.id = t.it

输出:

id  DescriptionE
2   Stone
3   Bar
5   s
6   b

仅供参考,如果您将MAX更改为MIN,那么您将得到ID为1,2,4,5

的答案

答案 6 :(得分:0)

你需要自我加入

http://www.mysqltutorial.org/mysql-self-join/

第一个表格将从表格

中选择*

第二张表将是差异

在mysql中我想出了....没有方便的mssql来试试

表t1 记录数量:3

col1    col2    col3
1   554412  Bar
2   554412  Stone
3   554412  Bar

SELECT
distinct
pv1.col2,  pv1.col3

from [t1] pv1
join [t1] pv2 on pv1.col1=pv2.col1

结果:

 Number of Records: 2
    col2    col3
    554412  Bar
    554412  Stone

ack,没有col1,对不起,已经玩了18个小时....尝试正确的加入

https://technet.microsoft.com/en-us/library/ms177490(v=sql.105).aspx