I have upgraded my MySQL server and now I have a problem with group by
select * from tickets WHERE `status` = 0 group by `tnumber` ORDER BY `created_at` DESC
error is:
SELECT list is not in GROUP BY clause and contains nonaggregated column
Updated: my table content is:
<!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>
My code is :
SELECT * FROM tickets
WHERE `tnumber` IN (
SELECT tnumber FROM tickets GROUP BY tnumber
)
but group by in second Select in where, doesn't have any affecs!!! How to do it in a propper way?
The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.
Based on your question, it looks like that the query was working properly prior to
MySql
upgrade. Since you've upgradedMySql
, default settings has been applied which meansOnly_Full_Group_By
is enabled. Hence, if you want to execute queries like the one in question where non-aggregated columns inSelect
list are not ingroup by
clause, you need to disableOnly_Full_Group_By
.Run below statement & execute your query again:
Warning: You should only do this if you're sure that other non-aggreegated columns in select list are unique for each
tnumber
otherwise you'll get random rows as a result.Following is quoted from MYSQL 8.0 Manual:
FOR More info, Click Here.
If you are grouping by a column, the result will be aggregated row(s). As the error message says, it can't display non-aggregated rows with aggregated column. You can restructure your query like this:
Update
If you want to fetch the record with MAX
id
then you can do the following:change your code from:
To: