Skip to content
Advertisement

Joining two table and get data from both table based on certain column in django

At first i want to make understand you to be not overactive or over-reactive without giving any solution and not understanding the core content and topics of questions.Here many people available who are expert at overacting and over-reacting by giving minus vote knowing anything.If you do not like this question then ignore it because your minus vote prevent many important questions from getting researched and answered.

However, have two table named ‘transaction’ and ‘member’. I want to join this two table like this :-

$select=$connect->query("SELECT transaction.id as id,transaction.date as date,member.name as name,member.address as address FROM transaction,member WHERE transaction.member_id=member.id ORDER  BY transaction.id ");
     while($data=$select->fetch_assoc()){

     echo $data['id'];
     echo $data['name'];
     //i echo it for your clearance 
     }

I need Django query from it which i will get same result.

Please don’t give any other questions reference something related to it.I need specific solution from this.

Advertisement

Answer

This select_related part will join the two tables (transaction, member) with condition (transaction.member_id=member.id ) then order_by(‘id’) will order the output with transaction.id

query = transaction.objects.select_related('member').order_by('id')

To print ids

print(list(query.values_list('id', flat=True))))

To print list on names

print(list(query.values_list('names', flat=True))))
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement