在MySql查询中决定WHERE子句

时间:2016-05-06 04:44:02

标签: php mysql pdo

我的数据库中有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
            )

menu表中没有tours

2 个答案:

答案 0 :(得分:0)

你试过加入吗?

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

答案 1 :(得分:0)

查询应该是别名

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