【问题标题】:How to conver a SQL query from MySQL 8 to MySQL 5.7如何将 SQL 查询从 MySQL 8 转换为 MySQL 5.7
【发布时间】:2021-12-29 14:31:54
【问题描述】:

如何将此代码修复到 MySQL 5.7?

with recursive u as
(select t.id, t.refferal, t.balance from users t where refferal = 1
union
select t.id, t.refferal, t.balance from u inner join users t
on u.id = t.refferal)
(select u.id,u.refferal,u.balance from u)

Fiddle

【问题讨论】:

  • 一种解决方法是使用 [now obsolete] 变量,但这不是万无一失的。另一种解决方案是编写存储过程。

标签: mysql sql


【解决方案1】:

灵感来自this 答案:

select  id,
        refferal,
        balance
from    (select * from users
         order by refferal, id) u,
        (select @pv := 1) v
where   find_in_set(refferal, @pv)
and     length(@pv := concat(@pv, ',', id))

Fiddle

【讨论】:

  • 感谢您的回答,但是如果 refferal dbfiddle.uk/…
  • @Pisit 它返回与您的其他查询相同的结果! fiddle
  • 你有没有办法在没有这个条件的情况下返回结果? (引用大于或小于 id)
  • @Pisit 也许您正在寻找的(在您的递归查询中)是 where t.id = 10 而不是 where t.refferal = 10 ?由于没有用户 referal = 10...
猜你喜欢
  • 2015-04-21
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-20
  • 2019-05-14
  • 2020-07-09
  • 2021-10-30
相关资源
最近更新 更多