简体   繁体   English

Mysql按列分组并最好在另一列值上获得最大id

[英]Mysql group by column and get max id preferably on another column value

I have a table with the content like this;我有一张表格,内容是这样的;

id      customer  is_default
1606384 5828      1
1573786 5828      0
1575316 5828      0
1817769 5828      0

For this customer, there is a is_default=1 address but some others there is none.对于这个客户,有一个is_default=1地址,但其他一些没有。 For example;例如;

id      customer  is_default
1806384 5829      0
1873786 5829      0
1875316 5829      0
1917769 5829      0

I'd like to get the MAX(id) but query should prefer is_default=1 first我想获得MAX(id)但查询应该首先选择is_default=1

So for example, in the first table query must return MAX(id) = 1606384 and for the second table should return MAX(id) = 1917769因此,例如,在第一个表中查询必须返回MAX(id) = 1606384而对于第二个表应返回MAX(id) = 1917769

I can't wrap my head around this.我无法解决这个问题。

Can you guys help me ?你们能帮帮我吗?

Edit 1编辑 1

Responding to comment of @Strawberry回应@Strawberry 的评论

I've tried this approach;我试过这种方法;

First I've multiplied the is_default with id and order by in order to get the max id from the query.首先,我将is_defaultid和 order by 相乘,以便从查询中获取最大 id。 But this was all wrong as now I understand但这一切都错了,因为现在我明白了

    SELECT MAX(id) as mid,MAX(is_default)
    FROM (
        (SELECT rel.maxid as id,customer,is_default FROM (
select (id+(is_default*10000000)) as maxid,customer,is_default FROM customer_address ORDER BY is_default DESC) rel ORDER BY maxid)
             ) rel
    GROUP BY customer
DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table 
(id SERIAL PRIMARY KEY
,customer INT NOT NULL
,is_default TINYINT NOT NULL
);

INSERT INTO my_table VALUES
(1606384,5828,1),
(1573786,5828,0),
(1575316,5828,0),
(1817769,5828,0),
(1806384,5829,0),
(1873786,5829,0),
(1875316,5829,0),
(1917769,5829,0);

SELECT x.customer
     , COALESCE(MAX(y.id),MAX(x.id)) id 
  FROM my_table x 
  LEFT 
  JOIN my_table y 
    ON y.customer = x.customer 
   AND y.is_default = 1 
 GROUP 
    BY x.customer;
+----------+---------+
| customer | id      |
+----------+---------+
|     5828 | 1606384 |
|     5829 | 1917769 |
+----------+---------+

You could do the following but @Strawberry answer seems way better :您可以执行以下操作,但@Strawberry 的答案似乎更好:

SELECT 
  customer, 
  (CASE WHEN is_default IS NOT NULL THEN is_default ELSE nis_default END) AS max
FROM (
SELECT 
  customer, 
  MAX(CASE WHEN is_default = 1 THEN id END) AS is_default, 
  MAX(CASE WHEN is_default = 0 THEN id END) AS nis_default
FROM t
GROUP BY customer) T

DEMO HERE 演示在这里

EDIT : My query can be simplified with COALESCE like this :编辑:我的查询可以用 COALESCE 简化,如下所示:

SELECT 
  customer, 
  COALESCE(
    MAX(CASE WHEN is_default = 1 THEN id END),
    MAX(CASE WHEN is_default = 0 THEN id END)) AS max
FROM t
GROUP BY customer

SECOND DEMO HERE 这里的第二个演示

If you just want single max(id) value, preferably with is_default=1, then use:如果您只想要单个 max(id) 值,最好使用 is_default=1,然后使用:

select max_id
from (
  select is_default, max(id) as  max_id
  from customer_address
  group by is_default
  order by is_default desc
  limit 1
) as q;

You need a CASE expression:你需要一个 CASE 表达式:

select 
  customer, 
  case max(is_default) 
    when 1 then max(is_default * id) 
    else max(id)
  end maxid
from tablename 
group by customer

See the demo .请参阅演示
Results:结果:

| customer | maxid   |
| -------- | ------- |
| 5828     | 1606384 |
| 5829     | 1917769 |
select MAX(ID),(select MAX(ID) from table where is_default = 0)
 from table
where is_default = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按用户 ID 对表进行分组并获得另一列的最大值? - How to group a table by user id and get max for another column? Mysql,如何使用另一列的最大值对一组行进行分组? - Mysql, How to group a set of rows by using the max value of another column? MYSQL获取另一列指定的组的列中的最小值 - MYSQL Get lowest value in column of a group specified by another column MySQL获取结果的最大ID,除非列中的值等于1 - MySQL Get Max ID of result UNLESS a value in a column equals 1 MySQL如何获取与同一列中另一个组的值匹配的组? - MySQL how to get the group that match the value of another group in the same column? mysql获取许多列组的最大值 - mysql getting max value of a column group by many Mysql - 删除所有“不是最大值”的列的值,其中 group by 在另一列上 - Mysql - delete all values that are `not the max` of one column with a group by on another column Mysql 按 id 对列组求和,然后将每个 id 的总和与另一个表中另一列的值相除,每行的 id 匹配 - Mysql sum a column group by id, then divide that sum for each id with value from another column in another table with the matching id for each row 获取每个唯一 ID 的最大列值 - Get the max column value for each unique ID MySQL使用另一列的最大值更新列值 - MySQL update column value with max value from another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM