Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 获取所有列的最常用值_Mysql - Fatal编程技术网

Mysql 获取所有列的最常用值

Mysql 获取所有列的最常用值,mysql,Mysql,我想获得mySQL表中所有列的最常见值 [id] [UserID] [UserID2] 1 johnnietheblack marywhite 2 johnnietheblack marywhite 3 dannyrottenegg dannyrottenegg 4 marywhite marywhite 5 marywhite johnnietheblac

我想获得mySQL表中所有列的最常见值

[id]  [UserID]            [UserID2]
1     johnnietheblack     marywhite
2     johnnietheblack     marywhite
3     dannyrottenegg      dannyrottenegg     
4     marywhite           marywhite
5     marywhite           johnnietheblack     
6     johnnietheblack     marywhite
以下是我想要的输出:

[id]  [UserID]            [Count]
    1     johnnietheblack     4
    2     dannyrottenegg      2
    3     marywhite           6
我可以使用以下方法获取一列的公共值。但是如何获得所有列的公共值呢

SELECT COUNT(*) AS `Rows`, UserID
FROM table-name
GROUP BY UserID
ORDER BY `Rows` DESC
我没有包括
id
列,因为它似乎与值不相关

如果要查看每个名称的所有ID,可以使用GROUP_CONCAT:

SELECT GROUP_CONCAT(id), UserID, COUNT(*) FROM
(SELECT id, UserID FROM tablename
 UNION ALL
 SELECT id, UserID2 FROM tablename) t
GROUP BY UserID

我不太清楚,你能解释不同的吗?您希望显示什么?错误:每个派生表都必须有自己的别名谢谢,现在它可以工作了。我试图在COUNT之前添加id列,但它返回了一个错误。我应该把它放在哪里?@WeContest,id列的值是任意的。你想要id做什么?不能使用原始id吗?@WeContest,johnnietheblack出现在4个不同的记录上。你想看哪个身份证?如果您想全部查看,请使用
GROUP\u CONCAT(id)
,否则,您必须指定您想要的。
SELECT GROUP_CONCAT(id), UserID, COUNT(*) FROM
(SELECT id, UserID FROM tablename
 UNION ALL
 SELECT id, UserID2 FROM tablename) t
GROUP BY UserID