Tried get "ORDER BY id DESC" in "GROUP BY" to get only last comment. When i use "IN" with >1 elements its works fine, but with one element or without "IN". My MariaDB version is 10.0.36-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04.
Here is examples: 56xxx - true ids, 55xxx false ids
MariaDB [shop]> SELECT `commentsapi_comment`.`id` FROM `commentsapi_comment`
WHERE (`commentsapi_comment`.`orderid`='6576') GROUP BY orderid DESC;
+-------+
| id |
+-------+
| 55811 |
+-------+
1 row in set (0.00 sec)
MariaDB [shop]> SELECT `commentsapi_comment`.`id` FROM `commentsapi_comment`
WHERE (`commentsapi_comment`.`orderid` IN ('6576')) GROUP BY orderid DESC;
+-------+
| id |
+-------+
| 55811 |
+-------+
1 row in set (0.00 sec)
MariaDB [shop]> SELECT `commentsapi_comment`.`id` FROM `commentsapi_comment`
WHERE (`commentsapi_comment`.`orderid` IN ('6576','6576')) GROUP BY orderid DESC;
+-------+
| id |
+-------+
| 56218 |
+-------+
1 row in set (0.00 sec)
MariaDB [shop]> SELECT `commentsapi_comment`.`id` FROM `commentsapi_comment`
WHERE (`commentsapi_comment`.`orderid` IN ('6576','-1')) GROUP BY orderid DESC;
+-------+
| id |
+-------+
| 56218 |
+-------+
1 row in set (0.01 sec)
MariaDB [shop]> SELECT `commentsapi_comment`.`id` FROM `commentsapi_comment`
WHERE (`commentsapi_comment`.`orderid` IN ('6576')) GROUP BY orderid DESC;
+-------+
| id |
+-------+
| 55811 |
+-------+
1 row in set (0.00 sec)
MariaDB [shop]> SELECT `commentsapi_comment`.`id` FROM `commentsapi_comment`
WHERE (`commentsapi_comment`.`orderid` IN ('6576','6577')) GROUP BY orderid DESC;
+-------+
| id |
+-------+
| 56199 |
| 56218 |
+-------+
2 rows in set (0.00 sec)
MariaDB [shop]> SELECT `commentsapi_comment`.`id` FROM `commentsapi_comment`
WHERE (`commentsapi_comment`.`orderid` IN ('6577')) GROUP BY orderid DESC;
+-------+
| id |
+-------+
| 55813 |
+-------+
1 row in set (0.01 sec)
Who knows reason of this?
That's an invalid use of GROUP BY
. Newer versions would spit at you.
When there are non-aggregate columns in the SELECT
(eg, id
) that are not in the GROUP BY
(which has only orderid
), the selected items are picked somewhat randomly.
Suggest you change id
to
orderid, MIN(id), MAX(id), COUNT(*), GROUP_CONCAT(id)
IN
is a red herring, not the cause.
If you need all the columns, not just id
, you need to say so in the Question. There are many Q&A about "grouwise-max".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.