Get value of all column from one table and get value of other column from second table

安稳与你 提交于 2019-12-24 07:28:14

问题


I need to get result from my database.

I have two table "travels" and "airlines" and I want to get result of all the columns from travels table which is ("id","name","airline_name","price","via") and from second table which is airlines based on the columns("id","name","logo").

I want to get "logo" whose name is the same as airline_name in the travel table.

What should I do? Should I used join?? So far my query is:

 $this->db->select();
 $this->db->from('travels');
 $this->db->join('airlines', 'travels.airline_name = airlines.name','inner');
 $this->db->group_by('travels.destination'); 

I am using CodeIgniter.


回答1:


SELECT
  t.id,
  t.name,
  t.airline_name,
  t.price,
  t.via,
  a.logo
FROM travels AS t
  INNER JOIN airlines AS a ON (t.airline_name = a.logo);



回答2:


Add this to your Model Function.and Get it to your controller

and Foreach in on your view

$this->db->select('t.id','t.name','t.airline_name','t.price','t.via', 'a.logo as a_logo')

; $this->db->join('airlines a', 'a.logo = t.airline_name');

$query = $this->db->get('travels t');
$query->return->result_array();


来源:https://stackoverflow.com/questions/41183506/get-value-of-all-column-from-one-table-and-get-value-of-other-column-from-second

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!