繁体   English   中英

Mysql查询中的Duel WHERE子句

[英]Duel WHERE clauses in MySql query

我的数据库中有2个表。

导航表

| id |   menu   | parent_id |
-----------------------------
| 1  | Thailand |     0     |
| 2  | Bangkok  |     1     |

游览表

| id |   parent_id   | city_id | name  |
----------------------------------------
| 1  |      1        |    2    | Tour 1
| 2  |      1        |    2    | Tour 2

我的查询语句是

$sql = DB::getInstance()->query(
    'SELECT 
        tours.id, 
        tours.parent_id, 
        tours.city_id, 
        tours.name, 
        navigation.id, 
        navigation.menu, 
        navigation.parent_id
    FROM 
        tours, navigation 
    WHERE 
        tours.parent_id = navigation.id'
);

这将返回以下内容

        [0] => stdClass Object
            (
                [id] => 1
                [parent_id] => 0
                [city_id] => 2
                [name] => RIVER KWAI ADVENTURE
                [price] => 18170
                [menu] => thailand
            )

我希望它返回以下内容

        [0] => stdClass Object
            (
                [id] => 1
                [parent_id] => 0
                [city_id] => 2
                [name] => RIVER KWAI ADVENTURE
                [price] => 18170
                [menu] => thailand
                [menu] => bangkok
            )

我如何在查询中执行此操作? 是决斗的WHERE子句吗?

编辑此

这就是我要返回的

        [0] => stdClass Object
            (
                [id] => 1
                [parent_id] => 0
                [city_id] => bangkok
                [name] => RIVER KWAI ADVENTURE
                [price] => 18170
                [menu] => thailand
            )

tours表中没有menu

您尝试过加入吗?

SELECT t.id AS [tourId], t.parent_id AS [tourParent_id], t.city_id, t.name, n.id as [navId], n.menu, n.parent_id AS [navParent_id]
FROM Tours t
JOIN Navigation n ON n.id = t.parent_id

查询应为别名

SELECT 
    tours.id, 
    tours.parent_id, 
    tours.city_id, 
    tours.name, 
    navigation.id, 
    navigation.menu as nav_menu, 
    tours.menu as tours_menu,
    navigation.parent_id
FROM 
    tours, navigation 
WHERE 
    tours.parent_id = navigation.id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM