简体   繁体   English

我想连接数据并显示一个表中的所有记录和另一个表中的一个记录

[英]I want to join data and display all records from one table and just one record from another table

I am a Codeigniter developer.我是一名 Codeigniter 开发人员。 I am working on an e-commerce site.我在一个电子商务网站上工作。 I face one issue in the admin panel.我在管理面板中遇到一个问题。 I have two tables table_1= tbl_category table_2= tbl_products我有两个表 table_1= tbl_category table_2= tbl_products

I want to update a product.我想更新一个产品。 I fetch all data of product from tbl_products using this:我使用这个从 tbl_products 获取产品的所有数据:

$this->db->select('*')
         ->from('tbl_products')
         ->where('cat_id,$id)
         ->get();

It is working perfect but I want to display a dropdown of all categories in the update product page.它运行良好,但我想在更新产品页面中显示所有类别的下拉列表。 The problem is I can't find a solution how I can do it.问题是我找不到解决办法,我该怎么做。 I have to fetch one product by its id from tbl product but how to fetch all categories.我必须通过 tbl 产品的 id 获取一个产品,但如何获取所有类别。 I want a solution in Codeigniter.我想要一个 Codeigniter 的解决方案。 I will wait for your answers Please help me, friends.我会等待你的答案 请帮助我,朋友。

you better remove the where clause or remove cat_id from where clause like:您最好删除 where 子句或从 where 子句中删除 cat_id ,例如:

$this->db->select('*')
         ->from('tbl_products')
         ->where('cat_id,$id) //remove where clause or remove cat_id
         ->get();

so that you will get all the categories selected以便您选择所有类别

if u want to display data from other table having a foreign key then use left join on the keys如果你想显示来自具有外键的其他表的数据,则在键上使用左连接

$this->db->select('*')
         ->from('tbl_products')
        ->join('tbl_category','tbl_category.id = tbl_products.id')
         ->get();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 php:对于一个mysql表中的每个记录,显示第二个表中的所有记录 - php: for each record in one mysql table, display all the records from a 2nd table 我想将数据从一张表移动到另一张表 - I Want to move data from one table to another 如何从一个表中选择数据并从另一表中获取所有相关记录? - How to select data from one table and get all associated records from another table? 如何使用联接仅从另一个表中获取一条记录 - How to get only one record from another table using Join 两个表一个包含记录,第二个包含位置我想显示第一个表记录而不连接 - Two tables one contain records and second conatain location i want to show first table record with out join 从一个表中获取不在另一个表中的所有数据 MySQL - Get all data from one table that's not in another table MySQL 连接同一数据库中另一表的一个完整表和一列,并使用 PHP 显示 - Join one complete table and one column from another table inside the same database and display with PHP 如何从一个表中获取现有记录的 ID 并将其与其他记录一起插入到另一个表中? - How to grab an ID of an existing record from one table and insert it along with other records into another table? 想要将数据从一个表插入到另一个在PHP中? - want to insert data from one table into another in php? 查询将一条记录(ID)从一张表插入到另一张表 - Query for insert one record (ID) from one table to another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM