使用选择语句的SQL连接查询 [英] SQL Joining query using Selection Statement

查看:70
本文介绍了使用选择语句的SQL连接查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个oc_category_description表,其中各列是:

I have one table of oc_category_description where columns are :

  • category_id
  • 名称

和其他表oc_category,其中列为:

and other table oc_category where columns are :

  • category_id
  • 图片
  • parent_id

这是oc_category_description表的示例图片

Here the sample pic of oc_category_description table

oc_category表

oc_category table

在这里我要显示名称,category_id,图像,parent_id,其中oc_category parent_id为0;

Here i am want to show name, category_id, image, parent_id where oc_category parent_id is 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;
}

但是它什么也没返回:(

but it returns nothing :(

推荐答案

在以下脚本中尝试此操作-

Try this below script-

SELECT oc_category.category_id, 
oc_category.image, 
oc_category.parent_id, 
oc_category_description.name
FROM oc_category 
INNER JOIN oc_category_description 
    ON oc_category.category_id = oc_category_description.category_id
WHERE oc_category.parent_id = 0 
ORDER BY oc_category.category_id ASC

这篇关于使用选择语句的SQL连接查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆