[英]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![]() |
active![]() |
shortform![]() |
country![]() |
type![]() |
---|---|---|---|---|---|
1 ![]() |
Uttar Pradesh![]() |
1 ![]() |
UP![]() |
India![]() |
State![]() |
2 ![]() |
Delhi![]() |
1 ![]() |
DELHI![]() |
India![]() |
Union territory![]() |
3 ![]() |
Punjab![]() |
1 ![]() |
PUN![]() |
India![]() |
State![]() |
4 ![]() |
Karnataka![]() |
1 ![]() |
KAR![]() |
India![]() |
State![]() |
5 ![]() |
Kerala![]() |
1 ![]() |
KER![]() |
India![]() |
State![]() |
6 ![]() |
Lucknow![]() |
1 ![]() |
LKO ![]() |
India![]() |
Capital![]() |
7 ![]() |
Chandigarh![]() |
1 ![]() |
CHA ![]() |
India![]() |
Capital![]() |
8 ![]() |
Bengaluru![]() |
1 ![]() |
BEN![]() |
India![]() |
Capital![]() |
9 ![]() |
Thiruvanthapuram![]() |
1 ![]() |
THI ![]() |
India![]() |
Capital![]() |
10 ![]() |
Saharanpur![]() |
1 ![]() |
SAH ![]() |
India![]() |
District![]() |
11 ![]() |
Meerut![]() |
1 ![]() |
MEE![]() |
India![]() |
District![]() |
12 ![]() |
Gorakhpur![]() |
1 ![]() |
GOR![]() |
India![]() |
District![]() |
and other named as place_capital和其他命名为 place_capital
id_place_place ![]() |
place_parent ![]() |
place_child ![]() |
---|---|---|
1 ![]() |
1 ![]() |
6 ![]() |
2 ![]() |
3 ![]() |
7 ![]() |
3 ![]() |
4 ![]() |
8 ![]() |
4 ![]() |
5 ![]() |
9 ![]() |
5 ![]() |
1 ![]() |
10 ![]() |
6 ![]() |
1 ![]() |
11 ![]() |
7 ![]() |
1 ![]() |
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![]() |
active![]() |
shortform![]() |
country![]() |
type![]() |
id_place_place ![]() |
place_parent ![]() |
place_child ![]() |
Group![]() |
---|---|---|---|---|---|---|---|---|---|
1 ![]() |
Uttar Pradesh![]() |
1 ![]() |
UP![]() |
India![]() |
State![]() |
1 ![]() |
1 ![]() |
6 ![]() |
6,10,11,12 ![]() |
3 ![]() |
Punjab![]() |
1 ![]() |
PUN![]() |
India![]() |
State![]() |
2 ![]() |
3 ![]() |
7 ![]() |
NULL![]() |
4 ![]() |
Karnataka![]() |
1 ![]() |
KAR![]() |
India![]() |
State![]() |
3 ![]() |
4 ![]() |
8 ![]() |
NULL![]() |
5 ![]() |
Kerala![]() |
1 ![]() |
KER![]() |
India![]() |
State![]() |
4 ![]() |
5 ![]() |
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![]() |
active![]() |
shortform![]() |
country![]() |
type![]() |
id_place_place ![]() |
place_parent ![]() |
place_child ![]() |
Group![]() |
---|---|---|---|---|---|---|---|---|---|
1 ![]() |
Uttar Pradesh![]() |
1 ![]() |
UP![]() |
India![]() |
State![]() |
1 ![]() |
1 ![]() |
6 ![]() |
6,10,11,12 ![]() |
2 ![]() |
Delhi![]() |
1 ![]() |
DELHI![]() |
India![]() |
Union Territory![]() |
NULL![]() |
NULL![]() |
NULL![]() |
NULL![]() |
3 ![]() |
Punjab![]() |
1 ![]() |
PUN![]() |
India![]() |
State![]() |
2 ![]() |
3 ![]() |
7 ![]() |
NULL![]() |
4 ![]() |
Karnataka![]() |
1 ![]() |
KAR![]() |
India![]() |
State![]() |
3 ![]() |
4 ![]() |
8 ![]() |
NULL![]() |
5 ![]() |
Kerala![]() |
1 ![]() |
KER![]() |
India![]() |
State![]() |
4 ![]() |
5 ![]() |
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.