[问]请帮忙,比如有一个数据
tbl_user
| Id | name |
| 1 | Bayu |
| 2 | Indra |
| 3 | Rangga |
tbl_data
| Id | userId | items |
| 1 | 1 | Knife |
| 2 | 3 | CD |
| 3 | 1 | Rose |
| 4 | 3 | HP |
| 5 | 2 | Honey |
| 6 | 1 | cups |
| 7 | 1 | Yarn |
| 8 | 2 | Shoes |
我想按项目排序表数据仅记录用户访问 例如,当用户Bayu登录时,数据的排序结果是顶部本身并且数据的持久化
userId | items |
1 | Yarn |
1 | cups |
1 | Knife |
1 | Rose |
2 | Honey |
2 | Shoes |
3 | CD |
3 | HP |
当Indra登录时排序的结果:
userId | items |
2 | Honey |
2 | Shoes |
1 | Yarn |
1 | cups |
1 | Knife |
1 | Rose |
3 | CD |
3 | HP |
或者当 Rangga 登录时
userId | items |
3 | CD |
3 | HP |
1 | Yarn |
1 | cups |
1 | Knife |
1 | Rose |
2 | Honey |
2 | Shoes |
如何查询(MySQL)选择它?
最佳答案
试试这个:
select userId, items
from tbl_data
order by userId=:userId desc, userId asc;
例如,如果 Indra 登录:
select userId, items
from tbl_data
order by userId=2 desc, userId asc;
关于mysql - 如何通过用户登录选择*订单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41499042/