如何从联接表中获取数据 [英] How to get data from a joined table

查看:46
本文介绍了如何从联接表中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,其中c.id = p.category_id. 我想获取category.name,但是它给出了一个错误. 谁能告诉我如何从联接表中获取数据?

I have two table joined with c.id = p.category_id. I want to get categories.name but it gives an error. Could anyone tell me how to get data from a joined table please?

function getGalleryone(){
     $data = array();
     $query = 'SELECT *
     FROM products AS p
     JOIN categories AS c
     ON c.id = p.category_id
     WHERE c.name = "Galleri1"
     AND p.status = "active"' ;
     $Q = $this->db->query($query);
     /*
     $this->db->select('*');
     $this->db->where('categories.name','Galleri 1');
     $this->db->where('products.status', 'active');
     $this->db->join('categories', 'categories.id = products.category_id');
     $this->db->order_by('name','random'); 
     $Q = $this->db->get('products');
     */
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data = array(
            "id" => $row['id'],
        "name" => $row['name'],
            "shortdesc" => $row['shortdesc'],
        ...
            ...
        "category" => $row['categories.name']
            );
       }
    }
    $Q->free_result();    
    return $data;  

数据库产品

CREATE TABLE IF NOT EXISTS `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `shortdesc` varchar(255) NOT NULL,
  `longdesc` text NOT NULL,
  `thumbnail` varchar(255) NOT NULL,
  `image` varchar(255) NOT NULL,
  `class` varchar(255) DEFAULT NULL,
  `grouping` varchar(16) DEFAULT NULL,
  `status` enum('active','inactive') NOT NULL,
  `category_id` int(11) NOT NULL,
  `featured` enum('true','false') NOT NULL,
  `price` float(4,2) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;

数据库类别

CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `shortdesc` varchar(255) NOT NULL,
  `longdesc` text NOT NULL,
  `status` enum('active','inactive') NOT NULL,
  `parentid` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
...
...

错误消息

A PHP Error was encountered

Severity: Notice

Message: Undefined index: categories.name

Filename: models/mproducts.php

Line Number: 111

谢谢.

推荐答案

categories.name列实际上将作为结果集中的$row['name']而不是$row['categories.name']返回.由于products也具有name列,一个将替换另一个.您应该指定要返回的字段,而不是使用*通配符选择每个字段.例如:

The categories.name column is actually going to be returned as just $row['name'] in the result set, not $row['categories.name']. Since products also has a name column, one is going to replace the other. Instead of selecting every field using the * wildcard, you should specify which fields you want returned. For example:

 SELECT p.*, c.name AS category
 FROM products AS p
 JOIN categories AS c
 ON c.id = p.category_id
 WHERE c.name = "Galleri1"
 AND p.status = "active"

然后您可以将类别名称引用为$row['category'].

Then you can reference the category name as $row['category'].

这篇关于如何从联接表中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆