简体   繁体   English

从与第一条记录匹配的表中选择第二条记录

[英]Select second record from table matching with first record

How select first record and matching with second record ?! 如何选择第一条记录并与第二条记录匹配?

Samlpe: Samlpe:

1- A
2- A
3- B
4- C

This code match 1 and 2 , and return TRUE 此代码匹配1和2,并返回TRUE

How write query ?! 怎么写查询?

To get the ID's of the rows that have the duplicate values on the second column you can do: 要获取第二列上具有重复值的行的ID,可以执行以下操作:

SELECT t1.id,t2.id
FROM tab1 t1
INNER JOIN tab1 t2 ON t1.val = t2.val AND t1.id <> t2.id;

To get TRUE when there are at least two rows that have the same value on the second column you or FALSE otherwise, you can do: 要在第二行中至少有两行具有相同的值时获得TRUE,否则为FALSE,则可以执行以下操作:

SELECT CASE 
    WHEN numEquals <> 0 THEN 'TRUE'
    ELSE 'FALSE' END AS HasEquals
FROM (
  SELECT COUNT(*) AS numEquals
  FROM tab1 t1
  INNER JOIN tab1 t2 ON t1.val = t2.val AND t1.id <> t2.id
  ) a

sqlfiddle demo sqlfiddle演示

basic sql 基本sql

SELECT * FROM table WHERE column1 = column2 LIMIT 1

in php you can do the check 在PHP中,你可以做检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM