php - 使用 join 命令 Codeigniter 不同的城市值

标签 php mysql codeigniter

我想从table_1获取post_id,其中User_id=5(它是65),然后我想要从table_2获取城市名称,其中post_id=65并区分城市值。我使用代码点火器。您能检查一下我下面的代码并帮我找出错误吗:

 public function get_city_list($user_id) {

$this->db->distinct();
$this->db->select('table_2.city');
$this->db->from('table_2');
$this->db->where('table_2.post_id=table_1.post_id');
$this->db->join('table_1','table_1.user_id = $user_id');
$query = $this->db->get();

}

最佳答案

$this->db->select('table_2.city');
$this->db->from('table_1');
$this->db->where('table_1.user_id',$user_id);
$this->db->join('table_2','table_1.post_id=table_2.post_id');
$this->db->get();

这将完成你的工作; 并且不要立即开始编写复杂的查询。从更简单的开始。 读得好,写得好

如果你想要独特的城市,试试这个

$this->db->distinct();
$this->db->get('table_2');

关于php - 使用 join 命令 Codeigniter 不同的城市值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25708304/

相关文章:

php - 如何从 PHP 中的 MySQL 查询获取循环中获取复选框值?

mysql - 运行时错误 3001 'Arguments are of the wrong type or out of acceptable range…' 插入 MySQL 数据库

php - php codeigniter中的电话号码验证

javascript - 下拉菜单打开另一个下拉菜单 - CodeIgniter

javascript - 如何用php重定向到主页

php - Mustache PHP - 如何从数据库加载数据?

php - 使用类映射时使 SOAP 客户端更具容错能力

mysql - Hive 外部表 - 数据位置元数据存储在哪里?

java - Spring - 如何在集成测试中将图像插入数据库

codeigniter - Codeigniter 的 sess_time_to_update 如何工作