选择具有超过4列匹配值的玩家

时间:2017-11-21 03:57:39

标签: php mysql sql

我想选择一个包含10列的表中的所有玩家,其中玩家是关键,如果他们有超过4个匹配的列。例如,如果col1,col2,col3和col4匹配,则列出具有相同列的所有用户,但如果col1,col5,col4和col8匹配,则列表列出具有相同列的所有用户。所以基本上如果玩家与其他玩家至少有4个匹配的列,无论col1 = col1还是col8 = col8都显示匹配列的用户

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
  <head>
    <title>logs</title>
    <meta name="GENERATOR" content="HeidiSQL 9.4.0.5125">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
      thead tr {background-color: ActiveCaption; color: CaptionText;}
      th, td {vertical-align: top; font-family: "Tahoma", Arial, Helvetica, sans-serif; font-size: 8pt; padding: 3px; }
      table, td {border: 1px solid silver;}
      table {border-collapse: collapse;}
      thead .col0 {width: 148px;}
      thead .col1 {width: 300px;}
      thead .col2 {width: 239px;}
      thead .col3 {width: 173px;}
      thead .col4 {width: 133px;}
      thead .col5 {width: 107px;}
      thead .col6 {width: 231px;}
      thead .col7 {width: 268px;}
      thead .col8 {width: 281px;}
    </style>
  </head>

  <body>

    <table caption="logs (7 rows)">
      <thead>
        <tr>
          <th class="col0">user</th>
          <th class="col1">gpuid</th>
          <th class="col2">cpuid</th>
          <th class="col3">winkey</th>
          <th class="col4">desktopname</th>
          <th class="col5">windowsuser</th>
          <th class="col6">bios</th>
          <th class="col7">processor</th>
          <th class="col8">video</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="col0">test1</td>
          <td class="col1">4d73e26e4a60043efcab3f4c2f084fec7b930c80784f2e5affabdab4d68f36f1</td>
          <td class="col2">13-1970169159-1818588270-1231384169</td>
          <td class="col3">00331-10000-00001-AA429</td>
          <td class="col4">TRUIX-DESKTOP</td>
          <td class="col5">Truix</td>
          <td class="col6">ALASKA - 1072009</td>
          <td class="col7">Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz</td>
          <td class="col8">NVIDIA GeForce GTX 1070</td>
        </tr>
        <tr>
          <td class="col0">test2</td>
          <td class="col1">4d73e26e4a60043efcab3f4c2f084fec7b930c80784f2e5affabdab4d68f36f1</td>
          <td class="col2">13-1970169159-1818588270-1231384169</td>
          <td class="col3">00331-10000-00001-AA429</td>
          <td class="col4">TRUIX-DESKTOP</td>
          <td class="col5">Truix</td>
          <td class="col6">ALASKA - 1072009</td>
          <td class="col7">Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz</td>
          <td class="col8">NVIDIA GeForce GTX 1070</td>
        </tr>
        <tr>
          <td class="col0">test3</td>
          <td class="col1">4d73e26e4a60043efcab3f4c2f084fec7b930c80784f2e5affabdab4d68f36f1</td>
          <td class="col2">13-1970169159-1818588270-1231384169</td>
          <td class="col3">00331-10000-00001-AA429</td>
          <td class="col4">TRUIX-DESKTOP</td>
          <td class="col5">Truix</td>
          <td class="col6">ALASKA - 1072009</td>
          <td class="col7">Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz</td>
          <td class="col8">NVIDIA GeForce GTX 1070</td>
        </tr>
        <tr>
          <td class="col0">test4</td>
          <td class="col1">d08b4d14ba5a5d296e228b193d7ec25af31a002b949ceb84e603b6e32f5cee80</td>
          <td class="col2">13-1970169159-1818588270-1231384169</td>
          <td class="col3">00330-80000-00000-AA369</td>
          <td class="col4">DESKTOP-E5L9057</td>
          <td class="col5">Thom</td>
          <td class="col6">ALASKA - 1072009</td>
          <td class="col7">Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz</td>
          <td class="col8">NVIDIA GeForce GTX 1080 Ti</td>
        </tr>
        <tr>
          <td class="col0">test5</td>
          <td class="col1">74421b33c2ececc0e9873acdc72f0f72dcbc6064b2fedea9590b28088b730e8f</td>
          <td class="col2">13-1970169159-1818588270-1231384169</td>
          <td class="col3">00330-80000-00000-AA682</td>
          <td class="col4">DESKTOP-412N9PE</td>
          <td class="col5">Skyler</td>
          <td class="col6">ALASKA - 1072009</td>
          <td class="col7">Intel(R) Core(TM) i5-4460  CPU @ 3.20GHz</td>
          <td class="col8">AMD Radeon R9 200 Series</td>
        </tr>
        <tr>
          <td class="col0">test6</td>
          <td class="col1">9fc2b8069c6c7cf3ff0ba01e93feeab14783345485a913e69462ee40d329f454</td>
          <td class="col2">6-1752462657-1145913699-1769238117</td>
          <td class="col3">00326-30000-00001-AA636</td>
          <td class="col4">DESKTOP-5PQDFP3</td>
          <td class="col5">Aiden</td>
          <td class="col6">HPQOEM - 1072009</td>
          <td class="col7">AMD A6-3650 APU with Radeon(tm) HD Graphics</td>
          <td class="col8">NVIDIA GeForce GTX 750 Ti</td>
        </tr>
        <tr>
          <td class="col0">truix</td>
          <td class="col1">4d73e26e4a60043efcab3f4c2f084fec7b930c80784f2e5affabdab4d68f36f1</td>
          <td class="col2">13-1970169159-1818588270-1231384169</td>
          <td class="col3">00331-10000-00001-AA429</td>
          <td class="col4">TRUIX-DESKTOP</td>
          <td class="col5">Truix</td>
          <td class="col6">ALASKA - 1072009</td>
          <td class="col7">Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz</td>
          <td class="col8">NVIDIA GeForce GTX 1070</td>
        </tr>
      </tbody>
    </table>


  </body>
</html>

我希望返回

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
  <head>
    <title>logs</title>
    <meta name="GENERATOR" content="HeidiSQL 9.4.0.5125">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
      thead tr {background-color: ActiveCaption; color: CaptionText;}
      th, td {vertical-align: top; font-family: "Tahoma", Arial, Helvetica, sans-serif; font-size: 8pt; padding: 3px; }
      table, td {border: 1px solid silver;}
      table {border-collapse: collapse;}
      thead .col0 {width: 148px;}
      thead .col1 {width: 300px;}
      thead .col2 {width: 239px;}
      thead .col3 {width: 173px;}
      thead .col4 {width: 133px;}
      thead .col5 {width: 107px;}
      thead .col6 {width: 231px;}
      thead .col7 {width: 268px;}
      thead .col8 {width: 281px;}
    </style>
  </head>

  <body>

    <table caption="logs (4 rows)">
      <thead>
        <tr>
          <th class="col0">user</th>
          <th class="col1">gpuid</th>
          <th class="col2">cpuid</th>
          <th class="col3">winkey</th>
          <th class="col4">desktopname</th>
          <th class="col5">windowsuser</th>
          <th class="col6">bios</th>
          <th class="col7">processor</th>
          <th class="col8">video</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="col0">test1</td>
          <td class="col1">4d73e26e4a60043efcab3f4c2f084fec7b930c80784f2e5affabdab4d68f36f1</td>
          <td class="col2">13-1970169159-1818588270-1231384169</td>
          <td class="col3">00331-10000-00001-AA429</td>
          <td class="col4">TRUIX-DESKTOP</td>
          <td class="col5">Truix</td>
          <td class="col6">ALASKA - 1072009</td>
          <td class="col7">Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz</td>
          <td class="col8">NVIDIA GeForce GTX 1070</td>
        </tr>
        <tr>
          <td class="col0">test2</td>
          <td class="col1">4d73e26e4a60043efcab3f4c2f084fec7b930c80784f2e5affabdab4d68f36f1</td>
          <td class="col2">13-1970169159-1818588270-1231384169</td>
          <td class="col3">00331-10000-00001-AA429</td>
          <td class="col4">TRUIX-DESKTOP</td>
          <td class="col5">Truix</td>
          <td class="col6">ALASKA - 1072009</td>
          <td class="col7">Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz</td>
          <td class="col8">NVIDIA GeForce GTX 1070</td>
        </tr>
        <tr>
          <td class="col0">test3</td>
          <td class="col1">4d73e26e4a60043efcab3f4c2f084fec7b930c80784f2e5affabdab4d68f36f1</td>
          <td class="col2">13-1970169159-1818588270-1231384169</td>
          <td class="col3">00331-10000-00001-AA429</td>
          <td class="col4">TRUIX-DESKTOP</td>
          <td class="col5">Truix</td>
          <td class="col6">ALASKA - 1072009</td>
          <td class="col7">Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz</td>
          <td class="col8">NVIDIA GeForce GTX 1070</td>
        </tr>
        <tr>
          <td class="col0">truix</td>
          <td class="col1">4d73e26e4a60043efcab3f4c2f084fec7b930c80784f2e5affabdab4d68f36f1</td>
          <td class="col2">13-1970169159-1818588270-1231384169</td>
          <td class="col3">00331-10000-00001-AA429</td>
          <td class="col4">TRUIX-DESKTOP</td>
          <td class="col5">Truix</td>
          <td class="col6">ALASKA - 1072009</td>
          <td class="col7">Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz</td>
          <td class="col8">NVIDIA GeForce GTX 1070</td>
        </tr>
      </tbody>
    </table>

    
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

如果我正确地阅读了您的问题,您希望找到在同一个表中超过4列共享相同值的记录。 由于GROUP BY和COUNT()将作为复合键使用,因此它只能找到所有已分组列的完全匹配。

我认为@Orangepill在评论中提到过。最简单的方法是将结果匹配的列值相对于同一个表上的连接相加以比较不同的值,然后使用DISTINCTHAVING限制匹配值的结果

SELECT DISTINCT 
 m.`user`,
 m.gpuid,
 m.cpuid,
 m.winkey,
 m.desktopname,
 m.windowsuser,
 m.bios,
 m.processor,
 m.video
FROM (
    SELECT p.*, (
      (p.gpuid = p2.gpuid) + 
      (p.cpuid = p2.cpuid) + 
      (p.winkey = p2.winkey) + 
      (p.desktopname = p2.desktopname) + 
      (p.windowsuser = p2.windowsuser) + 
      (p.bios = p2.bios) + 
      (p.processor = p2.processor) + 
      (p.video = p2.video)
    ) AS matches 
    FROM `table` AS p
    LEFT JOIN  `table` AS p2
    ON p.`user` != p2.`user`
    HAVING matches > 4
) AS m

结果http://sqlfiddle.com/#!9/180d5f/5

|  user |                                                            gpuid |                               cpuid |                  winkey |   desktopname | windowsuser |             bios |                                processor |                   video |
|-------|------------------------------------------------------------------|-------------------------------------|-------------------------|---------------|-------------|------------------|------------------------------------------|-------------------------|
| test2 | 4d73e26e4a60043efcab3f4c2f084fec7b930c80784f2e5affabdab4d68f36f1 | 13-1970169159-1818588270-1231384169 | 00331-10000-00001-AA429 | TRUIX-DESKTOP |       Truix | ALASKA - 1072009 | Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz | NVIDIA GeForce GTX 1070 |
| test3 | 4d73e26e4a60043efcab3f4c2f084fec7b930c80784f2e5affabdab4d68f36f1 | 13-1970169159-1818588270-1231384169 | 00331-10000-00001-AA429 | TRUIX-DESKTOP |       Truix | ALASKA - 1072009 | Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz | NVIDIA GeForce GTX 1070 |
| truix | 4d73e26e4a60043efcab3f4c2f084fec7b930c80784f2e5affabdab4d68f36f1 | 13-1970169159-1818588270-1231384169 | 00331-10000-00001-AA429 | TRUIX-DESKTOP |       Truix | ALASKA - 1072009 | Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz | NVIDIA GeForce GTX 1070 |
| test1 | 4d73e26e4a60043efcab3f4c2f084fec7b930c80784f2e5affabdab4d68f36f1 | 13-1970169159-1818588270-1231384169 | 00331-10000-00001-AA429 | TRUIX-DESKTOP |       Truix | ALASKA - 1072009 | Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz | NVIDIA GeForce GTX 1070 |