如何在MYSQL中按ID设置其他表的顺序

如何在MYSQL中按ID设置其他表的顺序,mysql,Mysql,我有两张表,下面给出了图片 tb_用户 tb\U用户\U连接 所以,我想从tb_用户_连接中获取记录,但按顺序 tb_user.first_name那么,如何设置此查询 尝试此查询。您的屏幕截图中缺少连接类型字段,并且不清楚您的逻辑是什么。是@TimBiegeleisen我现在就知道您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以了解在第3行“tbl_user where tbl_user.id=tb_user_connection.user_id as first_na

我有两张表,下面给出了图片

tb_用户

tb\U用户\U连接

所以,我想从tb_用户_连接中获取记录,但按顺序 tb_user.first_name那么,如何设置此查询


尝试此查询。

您的屏幕截图中缺少连接类型字段,并且不清楚您的逻辑是什么。是@TimBiegeleisen我现在就知道您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以了解在第3行“tbl_user where tbl_user.id=tb_user_connection.user_id as first_name_my from tb_”附近使用的正确语法等一下,让我检查一下。。。我认为子查询必须根据您的方法进行更改。亲爱的,我使用了tbl_用户。请更改它并使用您的表名tb用户。。请使用更新的sql查询重试。我根据我的方法更改,然后在“where子句”中为我提供未知列“tb\U user\U connection.user\U id”,请使用更新的sql查询重试。最后,它可能会起作用。
select * from tb_user_connection WHERE connection_type='a' 
AND user_id = '1' AND user_id <= connection_id
union all
select *
from tb_user_connection t
where user_id > connection_id 
and connection_type='a' and not exists (
select 1
from tb_user_connection t1
where t1.user_id = t.connection_id
and t1.connection_id = t.user_id 
)
SELECT  * 
FROM 
    (select *,(select first_name fron tb_user where tb_user.id =tb_user_connection.user_id) as first_name_my from tb_user_connection WHERE connection_type='a' 
AND user_id = '1' AND user_id <= connection_id
union all
select *,(select first_name fron tb_user where tb_user.id=t.user_id) as first_name_my
from tb_user_connection t
where user_id > connection_id 
and connection_type='a' and not exists (
select 1
from tb_user_connection t1
where t1.user_id = t.connection_id
and t1.connection_id = t.user_id 
)) dum
ORDER   BY first_name_my