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

Asked

Viewed 129 times

4

I take the category ID, and with it I search the subcategories. For this I am using WHERE’s. How would you look using INNER JOIN?

The WHERE’s thing got me confused

<?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;
            }
        }
    }

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();
  • returns nothing :(

  • ops, I got kkkk would be where admin_categorias.categoria = :category

  • 1

    I’m glad you did it even with my distraction, I’ll edit it to match your example. ^^

Browser other questions tagged

You are not signed in. Login or sign up in order to post.