简体   繁体   中英

How to find data between two columns with Sql?

I'm just learning Sql. I have a data with column names, for example: nkk, name, tps. How to find same 'nkk' data but different 'tps'

在此处输入图像描述

Use a self-join whose joining condition is exactly what you described: nkk is equal, but tps is not equal.

SELECT t1.nkk, t1.name AS name1, t1.tps AS tps1, t2.name AS name2, t2.tps AS tps2
FROM your_table AS t1
JOIN your_table AS t2 ON t1.nkk = t2.nkk AND t1.tps < t2.tps

I use < rather than != so we only get one of each pair. Otherwise you'll get both Aa Ac and Ac Aa .

Try this query,


    SELECT nkk, count(DISTINCT tps) AS c 
        FROM `tblname` 
        GROUP BY nkk 
        HAVING c > 1 
        ORDER BY c DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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