gpt4 book ai didi

php - 从相关表中获取值

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

我正在尝试寻找一条包含 2 个城市的路线。我有表“route”、“city”和“route_city”。输入时我有两个城市 ID(出发地和目的地)。我的 table :

城市

id name
1 Kiev
2 Rome

路线

id name
1 Kiev-Rome
2 Rome-Kiev

路线城市

id route_id city_id sort
1 1 1 1
2 1 2 2
3 2 2 1
4 2 1 2

我尝试过:

SELECT `route_id` FROM `route_city` WHERE (`route_city`.`city_id` IN ('1', '2')) GROUP BY `route_city`.`route_id`

如果我有 from=Kiev 和 to=Rome,我会得到两条路线。

1)如何才能只获得一条路线?

2)我如何使用 ActiveRecord 在 Yii2 框架中创建此查询?

最佳答案

sort 列是您的 fromto 代码吗?像这样:如果 sort=1,则为 from,如果 sort=2,则为 to

如果是,从=基辅到=罗马,您可以这样做:

SELECT `route_id` FROM `route_city` WHERE (`route_city`.`city_id` IN ('1', '2')) AND `route_city`.`sort`=1 GROUP BY `route_city`.`route_id`

对于 Yii,我无法提供任何帮助!

关于php - 从相关表中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47681579/

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