问题
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