简体   繁体   中英

get category name from category id in different table

I have two tables.

One table is animals. Second table is animal_details.

I am printing animals to table with loop.

Animals table:
id: 1
name: Dog

Animal Details table:
id: 1
name: Lorem ipsum
description: lorem ipsum dolor
animal: 1

I just want when i print details, i want echo animal with name (animal coming with id). I can do this with using second query in the loop but i want to do this without using query in loop. Thanks!

如果每个动物只有一个匹配行,最好在动物详细信息表中执行以下操作:

select * from animals, animalDetails where animal.id = animalDetails.animal

Simple use inner join

SELECT animal_details.description,animal.name,animal_details.name as detailsname
FROM animals INNER JOIN animal_details ON animals.id = animal_details.animal

You can just use a join query

SELECT animals . , animal_details . FROM animals , animal_details where animals.id=animal_details.animal;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM