Consider the following table:

考虑下面的表格:

mysql> desc foo;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| name       | varchar(255) | NO   |     | NULL    |                |
| created_at | datetime     | NO   |     | NULL    |                |
| updated_at | datetime     | NO   |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> desc bar;
+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| foo_id          | int(11)      | NO   | MUL | NULL    |                |
| status          | varchar(255) | YES  |     | NULL    |                |
| created_at      | datetime     | NO   |     | NULL    |                |
| updated_at      | datetime     | NO   |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

I want to fetch the recently updated foo_id and status from the bar table and the foo_id consist of multiple status as well

我想从bar表获取最近更新的foo_id和状态,而foo_id也包含多个状态

I tried following query:

我试着以下查询:

SELECT foo_id, status
FROM bar d1
WHERE d1.updated_at =
    (SELECT MAX(d2.updated_at)
     FROM bar d2
     WHERE d1.foo_id = d2.foo_id)
GROUP BY foo_id
ORDER BY updated_at DESC;

Got the following error on different machine:

在不同的机器上得到以下错误:

Unhandled rejection SequelizeDatabaseError: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'dev.d1.status' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

未处理的拒绝SequelizeDatabaseError: SELECT list的表达式#2不在GROUP BY子句中,并且包含非聚合列'dev.d1。不依赖于GROUP BY子句中的列的状态;这与sql_mode=only_full_group_by不兼容

How do I fetch the latest status of each foo_id from bar table?

如何从bar表获取每个foo_id的最新状态?

1 个解决方案

#1


3

If you just want the id of the max updated_at you could use

如果您只想要max updated_at的id,可以使用它

select foo_id, status 
from bar 
where updated_at = (
select MAX(updated_at) 
from bar 
)

or using a join

或者使用一个连接

select foo_id, status 
from bar 
INNER JOIN (
select MAX(updated_at)  max_date
from bar 
) t on t.max_date  = bar.updated_at 

更多相关文章

  1. 使用MySQL正则表达式 __MySQL必知必会
  2. MySQL搜索优化(用子查询替换长正则表达式)
  3. 为什么我们的查询卡在MySql中的“写入网络”状态?
  4. JS正则表达式中有哪些特殊字符,这些特殊字符又分别表示什么含义?
  5. 确定mysql中索引的状态
  6. 语法错误:从[{id}]开始的表达式[{id}]第2列的令牌'{'无效键?
  7. RegExp:匹配除Javascript中的正则表达式值之外的所有内容
  8. Javascript正则表达式对象和美元符号
  9. 密码强度正则表达式与数字[重复]

随机推荐

  1. 在asp.net中捕获href事件
  2. jQuery Ajax 方法调用 Asp.Net WebServic
  3. 如何使用自定义标头进行AJAX POST跨域
  4. 如何删除图例饼图中的标题?
  5. jquery datepicker,即使是另一个具有共享
  6. 为什么这个jQuery。ajax不会引发错误吗?
  7. Jquery禁用/启用按钮与文本框代码
  8. jQuery中的bind绑定事件与文本框改变事件
  9. jQuery实例:输入框下拉提示,仿google sugg
  10. 从内部获取函数名称