楼主您好
select
max(sum)
from(
select
该列,count(1)
sum
from
table
group
by
该列
having
count(1)>1)
如果结果大于1,则表示存在相同记录
CREATE TABLE message (
id int,
name1 varchar(2),
data1 int,
name2 varchar(2),
data2 int
);
INSERT INTO message
SELECT 1, 'A', 4, 'A', 1 UNION ALL
SELECT 2, 'B', 3, 'C', 2 UNION ALL
SELECT 3, 'A', 3, 'B', 4 UNION ALL
SELECT 4, 'B', 2, 'D', 6 UNION ALL
SELECT 5, 'C', 5, 'A', 4 UNION ALL
SELECT 6, 'A', 4, 'C', 7 UNION ALL
SELECT 7, 'A', 5, 'A', 8 UNION ALL
SELECT 8, 'B', 9, 'A', 3 UNION ALL
SELECT 9, 'B', 7, 'B', 4 UNION ALL
SELECT 10, 'C', 8, 'C', 5;
SELECT
my_data,
CONCAT('(', GROUP_CONCAT(my_name), ')') AS names
FROM
(
SELECT 'name1' as my_name, data1 as my_data FROM message WHERE name1 = 'A'
UNION
SELECT 'name2' as my_name, data2 as my_data FROM message WHERE name2 = 'A'
) subQuery
GROUP BY
my_data
ORDER BY
my_data DESC;
+---------+---------------+
| my_data | names |
+---------+---------------+
| 8 | (name2) |
| 5 | (name1) |
| 4 | (name1,name2) |
| 3 | (name1,name2) |
| 1 | (name2) |
+---------+---------------+
5 rows in set (000 sec)
以上就是关于SQL语句来判断数据库某一列下是否有相同值的记录方法全部的内容,包括:SQL语句来判断数据库某一列下是否有相同值的记录方法、SQL数据库,select两个字段的非重复值并且排序、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)