使用选择语句的SQL连接查询

编程入门 行业动态 更新时间:2024-10-27 09:32:15
本文介绍了使用选择语句的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连接查询

本文发布于:2023-10-22 08:09:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1516947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!