douzuita7325 2018-07-03 07:16
浏览 27

如何在Codeigniter和mysql中进行此查询? [重复]

This question already has an answer here:

Suppose there are two tables i.e product and product_status

here is the structure of product table

-------------------------------------------------
|   ID   |   PRODUCT_NAME   |    PRODUCT_DESC   |
-------------------------------------------------
|   1    |      abc         |    abc desc       |
-------------------------------------------------
|   2    |      xyz         |    xyz desc       |
-------------------------------------------------

here is the structure of product_status table

-------------------------------------------------
|   ID   |   PRODUCT_ID     |    PRODUCT_STATUS |
-------------------------------------------------
|   1    |        1         |      status 1     |
-------------------------------------------------
|   2    |        1         |      status 2     |
-------------------------------------------------

If I make a query through php (codeigniter) then it will show the data like this.

$this->db->where('ID',1);
$query = $this->db->get('product');
$product = $query->row();

echo $product->PRODUCT_NAME;

But my question is how to get status from product_status table for that specific product id without doing another query? I mean I want a single query which will fetch a single record (because id is unique) and show the product data from product table as well as product status from product_status table as obejct array like this-

$products = $product->PRODUCT_STATUS;

So I can loop through to $products and print the status.

foreach ($products as $p) {
    echo $p;
}

Is it possible without making another query?

</div>
  • 写回答

1条回答 默认 最新

  • doutouhe5343 2018-07-03 07:24
    关注
    $sql = sprintf("SELECT p.*, ps.PRODUCT_STATUS FROM product p LEFT JOIN product_status ps ON(p.ID = ps.PRODUCT_ID) WHERE p.ID=%d", $product_id);
    $query = $this->db->query($sql);
    $product = $query->row();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)