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

Posted on

Coding Problem :

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 ApplicationModels;
    use ApplicationModelsModel;

    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

Answer :


Answer 1 :

$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();

    


Answer 2 :


Answer 3 :


Answer 4 :


Answer 5 :


Answer 6 :


Answer 7 :


Answer 8 :


Answer 9 :

Leave a Reply

Your email address will not be published.