Skip to content
Advertisement

How to combine these two SELECT statment into one

I have two SELECT query which are written in MySQL

$sql = 'SELECT Name FROM category WHERE CategoryID = '.$id;

AND

$sql = "SELECT * FROM sub_category WHERE ParentID = $id ORDER BY OrderIndex";

So far I have to translate these two query to Oracle PL/SQL‚I came in idea to combine these two statment into one and so far I write query but I am a little bit confusing since I am begginer in PL/SQL

SELECT 
  c.Name,
  sc.Name as SubCategoryName,
  sc.ID as SubCategoryID,
  sc.Picture as Picture,
  sc.OrderIndex
FROM Category c
INNER JOIN sub_category sc ON ParentID =
WHERE CategoryID = 23;

Here is graphical representation of tables

CategoryTable

enter image description here

SubcategoryTable

enter image description here

Advertisement

Answer

Your idea is correct, joining tables is one of the standard ways to “combine two select statements together”. You are going to get different results depending on the kind of join you use. See for instance this question for an explanation of the different kind of joins.

Your query is almost correct (assuming you want an inner join), you are missing the right hand side on the join condition:

SELECT 
  c.Name,
  sc.Name as SubCategoryName,
  sc.ID as SubCategoryID,
  sc.Picture as Picture,
  sc.OrderIndex
FROM Category c
INNER JOIN sub_category sc ON sc.ParentID = c.CategoryID
WHERE CategoryID = 23;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement