How to do a SELECT between two tables and their WHERE's?

4

I get the category ID, and with it I look for the subcategories. For this I am using WHERE's. How would you use INNER JOIN?

The WHERE's question confused me

<?php
    namespace Application\Models;
    use Application\Models\Model;

    class ComboModel extends Model
    {
        public function subcategoria(){
            $args = func_get_args();
            if(count($args) == 1 && is_array($args[0])){
                $categoria = strip_tags($args[0]['categoria']);

                $id_categoria = $this->db->prepare("SELECT 'id' FROM 'admin_categorias' WHERE 'categoria' = :categoria");
                $id_categoria->bindValue(":categoria", $categoria, \PDO::PARAM_STR);
                $id_categoria->execute();
                $id = $id_categoria->fetchObject();

                $subcategorias = $this->db->prepare("SELECT 'subcategoria' FROM 'admin_subcategorias' WHERE 'id_categoria' = :id ORDER BY 'subcategoria' ASC");
                $subcategorias->bindValue(":id", $id->id, \PDO::PARAM_INT);
                $subcategorias->execute();

                return $subcategorias->fetchAll();
            } else {
                return false;
            }
        }
    }
    
asked by anonymous 29.01.2017 / 04:23

1 answer

6
$subcategorias = $this->db->prepare("SELECT 'subcategoria' FROM 'admin_categorias'
                                      INNER JOIN 'admin_subcategorias' ON 'admin_subcategorias'.'id_categoria' =  'admin_categorias'.'id'
                                      WHERE 'admin_categorias'.'categoria' = :categoria");
 $subcategorias->bindValue(":categoria", $categoria, \PDO::PARAM_STR);
 $subcategorias->execute();

 return  $subcategorias->fetchAll();
    
29.01.2017 / 05:41