SQL Select 语句加入
SQL Select Statement joining
我有一个 table of oc_category_description 其中列是:
- category_id
- 姓名
和其他 table oc_category 其中列为:
- category_id
- 图片
- parent_id
这里是oc_category_descriptiontable
的示例图片
oc_category table
这里我想显示名字,category_id,图片,parent_id 其中 oc_category parent_id 是 0;
这是sql:
php
function getMainCategory()
{
$stmt = $this->con->prepare("SELECT category_id, image, parent_id, (SELECT oc_category_description.name FROM oc_category_description WHERE oc_category.category_id = oc_category_description.category_id) FROM oc_category WHERE parent_id = 0 ORDER BY category_id ASC");
$stmt->execute();
$stmt->bind_result($category_id, $image, $parent_id, $name);
$users = array();
while ($stmt->fetch()) {
$temp = array();
$temp['category_id'] = $category_id;
$temp['image'] = $image;
$temp['parent_id'] = $parent_id;
$temp['name'] = $name;
array_push($users, $temp);
}
return $users;
}
但是returns没什么:(
使用加入
SELECT ocd.category_id, image, parent_id, name
FROM oc_category_description AS ocd
INNER JOIN oc_category
WHERE parent_id = 0
ORDER BY ocd.category_id ASC
使用 INNER JOIN:
SELECT d.category_id, c.image, c.parent_id, d.name
FROM oc_category_description d INNER JOIN oc_category c
ON c.category_id = d.category_id
WHERE c.parent_id = 0
ORDER BY d.category_id
如果结果中出现重复行,请更改为:
SELECT DISTINCT...
我有一个 table of oc_category_description 其中列是:
- category_id
- 姓名
和其他 table oc_category 其中列为:
- category_id
- 图片
- parent_id
这里是oc_category_descriptiontable
的示例图片oc_category table
这里我想显示名字,category_id,图片,parent_id 其中 oc_category parent_id 是 0;
这是sql:
php
function getMainCategory()
{
$stmt = $this->con->prepare("SELECT category_id, image, parent_id, (SELECT oc_category_description.name FROM oc_category_description WHERE oc_category.category_id = oc_category_description.category_id) FROM oc_category WHERE parent_id = 0 ORDER BY category_id ASC");
$stmt->execute();
$stmt->bind_result($category_id, $image, $parent_id, $name);
$users = array();
while ($stmt->fetch()) {
$temp = array();
$temp['category_id'] = $category_id;
$temp['image'] = $image;
$temp['parent_id'] = $parent_id;
$temp['name'] = $name;
array_push($users, $temp);
}
return $users;
}
但是returns没什么:(
使用加入
SELECT ocd.category_id, image, parent_id, name
FROM oc_category_description AS ocd
INNER JOIN oc_category
WHERE parent_id = 0
ORDER BY ocd.category_id ASC
使用 INNER JOIN:
SELECT d.category_id, c.image, c.parent_id, d.name
FROM oc_category_description d INNER JOIN oc_category c
ON c.category_id = d.category_id
WHERE c.parent_id = 0
ORDER BY d.category_id
如果结果中出现重复行,请更改为:
SELECT DISTINCT...