D
D
DeniSidorenko2020-03-01 09:45:04
PHP
DeniSidorenko, 2020-03-01 09:45:04

How to display category names from another table knowing the ID?

Hello, tell me how best to do it.

There are 2 tables product, category
When adding a product, it is selected from existing categories and in the product table there is a category_id that has a category id.

When displaying products, I can output category_id( int) but how do I get the category names. ( The category table has only id and name )

Is it necessary to cast into the database to the category by id inside the while loop and get the name
or is it possible in another way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Ushenin, 2020-03-01
@DeniSidorenko

SELECT
    product.*,
    category.name
FROM product p
JOIN category c
    ON c.id = p.category_id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question