简体   繁体   English

我在 php 文件中的 MYSQL 代码查询有什么问题?

[英]What's wrong with my MYSQL Code query in php file?

I have two tables in mysql database我在mysql数据库中有两个表

one table named is place:一张名为地点的表:

place_id place_id place地方 active积极的 shortform简写 country国家 type类型
1 1 Uttar Pradesh北方邦 1 1 UP向上 India印度 State状态
2 2 Delhi德里 1 1 DELHI德里 India印度 Union territory联盟领土
3 3 Punjab旁遮普 1 1 PUN双关语 India印度 State状态
4 4 Karnataka卡纳塔克邦 1 1 KAR卡尔 India印度 State状态
5 5 Kerala喀拉拉邦 1 1 KER克尔 India印度 State状态
6 6 Lucknow勒克瑙 1 1 LKO LKO India印度 Capital首都
7 7 Chandigarh昌迪加尔 1 1 CHA CHA India印度 Capital首都
8 8 Bengaluru班加罗尔 1 1 BEN India印度 Capital首都
9 9 Thiruvanthapuram蒂鲁文塔布勒姆 1 1 THI THI India印度 Capital首都
10 10 Saharanpur撒哈拉布尔 1 1 SAH SAH India印度 District
11 11 Meerut密拉特 1 1 MEE中东欧 India印度 District
12 12 Gorakhpur戈拉赫布尔 1 1 GOR戈尔 India印度 District

and other named as place_capital和其他命名为 place_capital

id_place_place id_place_place place_parent place_parent place_child place_child
1 1 1 1 6 6
2 2 3 3 7 7
3 3 4 4 8 8
4 4 5 5 9 9
5 5 1 1 10 10
6 6 1 1 11 11
7 7 1 1 12 12

I run this query我运行这个查询

select *, group_concat(place_capital.place_child) AS Group from place
left join place_capital on place.place_id = place_place.place_parent
where place.place_id = place_place.place_parent and active = :active AND type = :type
group by place ORDER BY place

and it give the following result它给出了以下结果

place_id place_id place地方 active积极的 shortform简写 country国家 type类型 id_place_place id_place_place place_parent place_parent place_child place_child Group团体
1 1 Uttar Pradesh北方邦 1 1 UP向上 India印度 State状态 1 1 1 1 6 6 6,10,11,12 6,10,11,12
3 3 Punjab旁遮普 1 1 PUN双关语 India印度 State状态 2 2 3 3 7 7 NULL无效的
4 4 Karnataka卡纳塔克邦 1 1 KAR卡尔 India印度 State状态 3 3 4 4 8 8 NULL无效的
5 5 Kerala喀拉拉邦 1 1 KER克尔 India印度 State状态 4 4 5 5 9 9 NULL无效的

What query I used in php file to give the following result that include Delhi also.我在 php 文件中使用了什么查询来给出以下结果,其中也包括德里 The place table have some relationship with place_place table . place 表place_place 表有一些关系。 with my above sql query that not give the Delhi in the result.使用我上面的 sql 查询,结果中没有给出德里。 So, what is the thing I missed in the php file to get the below result.那么,为了得到以下结果,我在 php 文件中遗漏了什么。

place_id place_id place地方 active积极的 shortform简写 country国家 type类型 id_place_place id_place_place place_parent place_parent place_child place_child Group团体
1 1 Uttar Pradesh北方邦 1 1 UP向上 India印度 State状态 1 1 1 1 6 6 6,10,11,12 6,10,11,12
2 2 Delhi德里 1 1 DELHI德里 India印度 Union Territory联合领土 NULL无效的 NULL无效的 NULL无效的 NULL无效的
3 3 Punjab旁遮普 1 1 PUN双关语 India印度 State状态 2 2 3 3 7 7 NULL无效的
4 4 Karnataka卡纳塔克邦 1 1 KAR卡尔 India印度 State状态 3 3 4 4 8 8 NULL无效的
5 5 Kerala喀拉拉邦 1 1 KER克尔 India印度 State状态 4 4 5 5 9 9 NULL无效的

and here is the code in the php file:-这是php文件中的代码:-

public function __construct(Querier $db, $type = "", $active = 1) {
    $this->db = $db;
    $this->type = $type;
    $this->active = $active;

    $connection = $this->db->getConnection();

    if ($this->type != "") {
        $statement = $connection->prepare("select *, group_concat(place_capital.place_child) AS Group from place
                                        left join place_capital on place.place_id = place_place.place_parent
                                        where place.place_id = place_place.place_parent and active = :active AND type = :type
                                        group by place ORDER BY place ");
        $statement->bindParam(":type", $this->type);
    } else {
        $statement = $connection->prepare("SELECT * FROM place WHERE active = :active ORDER BY place ");
    }

    $statement->bindParam(":active", $this->active);
    $statement->execute();
    $this->_guide_list = $statement->fetchAll();

}

As I can see here you only filter based on state and Delhi is Union Territory so to include that in a list you need to use In operator to filter type.正如我在这里看到的,您仅根据州进行过滤,而德里是联合领土,因此要将其包含在列表中,您需要使用 In 运算符来过滤类型。 you can also skip a join condition from where clause.您还可以从 where 子句中跳过连接条件。

SELECT *,
   Group_concat(place_capital.place_child) AS Group
FROM   place
   left join place_capital
          ON place.place_id = place_capital.place_parent
WHERE active = 1
   AND TYPE IN ('State', 'Union Territory')
GROUP  BY place.place_id
ORDER  BY place.place_id

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

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