在 mysql 8 中使用 GROUP BY

using of GROUP BY in mysql 8

我已经升级了我的 MySQL 服务器,现在我遇到了分组问题

select * from tickets WHERE `status` = 0 group by `tnumber` ORDER BY `created_at` DESC

错误是:

SELECT list is not in GROUP BY clause and contains nonaggregated column

更新: 我的 table 内容是:

        <!DOCTYPE html>
        <html>
        <head>
        <style>
        table {
          font-family: arial, sans-serif;
          border-collapse: collapse;
          width: 100%;
        }

        td, th {
          border: 1px solid #dddddd;
          text-align: left;
          padding: 8px;
        }

        tr:nth-child(even) {
          background-color: #dddddd;
        }
        </style>
        </head>
        <body>

        <h2>id</h2>

        <table>
          <tr>
            <th>id</th>
            <th>tnumber</th>
            <th>title</th>
            <th>status</th>
          </tr>
          <tr>
            <td>1</td>
            <td>5c63e00a072c1</td>
            <td>1111</td>
            <td>1</td>
          </tr>
          <tr>
            <td>2</td>
            <td>5c63e00a072c1</td>
            <td>1111</td>
            <td>1</td>
          </tr>
          <tr>
            <td>3</td>
            <td>5c63e00a072c1</td>
            <td>1111</td>
            <td>1</td>
          </tr>
          <tr>
            <td>4</td>
            <td>5c63e00a072c1</td>
            <td>1111</td>
            <td>0</td>
          </tr>
          <tr>
            <td>5</td>
            <td>5c63ead5bd530</td>
            <td>2222</td>
            <td>1</td>
          </tr>
          <tr>
            <td>6</td>
            <td>5c63ead5bd530</td>
            <td>2222</td>
            <td>1</td>
          </tr>
        </table>

        <br/>
        I want to have following output:
        <br/>
        <br/>
        <br/>
        <table>
          <tr>
            <th>id</th>
            <th>tnumber</th>
            <th>title</th>
            <th>status</th>
          </tr>
          <tr>
            <td>1</td>
            <td>5c63e00a072c1</td>
            <td>1111</td>
            <td>0</td>
          </tr>
          <tr>
            <td>2</td>
            <td>5c63ead5bd530</td>
            <td>222</td>
            <td>1</td>
          </tr>
        </table>
        </body>
        </html>

我的代码是:

SELECT * FROM tickets 
WHERE `tnumber` IN ( 
                      SELECT tnumber FROM tickets GROUP BY tnumber
                   )  

但是group by在second Select where,没有任何影响!!! 怎么做才合适?

GROUP BY 语句通常与聚合函数(COUNT、MAX、MIN、SUM、AVG)一起使用,以按一列或多列对结果集进行分组。

根据您的问题,在 MySql 升级之前,查询似乎工作正常。由于您已升级 MySql,已应用默认设置,这意味着 Only_Full_Group_By 已启用。因此,如果你想执行类似 Select 列表中的非聚合列不在 group by 子句中的查询,你需要禁用 Only_Full_Group_By.

运行 下面的语句并再次执行您的查询:

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

警告:只有当您确定 select 列表中的其他非聚合列对于每个 tnumber 都是唯一的时,您才应该这样做,否则您将得到随机行。

以下引用自MYSQL 8.0 手册:

MySQL implements detection of functional dependence. If the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default), MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them.

If ONLY_FULL_GROUP_BY is disabled, a MySQL extension to the standard SQL use of GROUP BY permits the select list, HAVING condition, or ORDER BY list to refer to nonaggregated columns even if the columns are not functionally dependent on GROUP BY columns. This causes MySQL to accept the preceding query. In this case, the server is free to choose any value from each group, so unless they are the same, the values chosen are nondeterministic, which is probably not what you want. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. Result set sorting occurs after values have been chosen, and ORDER BY does not affect which value within each group the server chooses. Disabling ONLY_FULL_GROUP_BY is useful primarily when you know that, due to some property of the data, all values in each nonaggregated column not named in the GROUP BY are the same for each group.

有关更多信息,Click Here

如果您按列分组,结果将聚合行。正如错误消息所说,它无法显示具有聚合列的非聚合行。您可以像这样重构您的查询:

SELECT `tnumber`
from tickets 
WHERE `status` = 0 
group by `tnumber` 
ORDER BY `tnumber` DESC

更新

如果你想获取MAX id的记录,那么你可以这样做:

SELECT `usrid` , `tnumber` , `title` , `message` , `status` , `created_at` , `type` 
FROM tickets 
WHERE status = 0
AND `id` IN (
    SELECT MAX(`id`)
    FROM tickets
);

更改您的代码:

select * from tickets WHERE `status` = 0 group by `tnumber`;

收件人:

SELECT * from tickets where id in (SELECT max(id) FROM tickets GROUP BY tnumber) AND `status` = 0;