gpt4 book ai didi

mysql - 如何通过用户登录选择*订单?

转载 作者:行者123 更新时间:2023-11-29 07:06:18 28 4
gpt4 key购买 nike

[问]请帮忙,比如有一个数据

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com