P
P
pofeg2020-11-20 23:20:51
SQL
pofeg, 2020-11-20 23:20:51

How to properly collect php request to display content by id?

There are 2 columns in the database:
site_category and site_news
The site_category contains the categories I created and recorded by type:
The category column contains entries of this type:

name -> Химия
category_id -> 1
------------------------
name - > Физика
category_id -> 2

i.e. there are 2 records in the database with two categories that have their own id

. And the site_news column with news contains the following records:
title -> Заголовок первой новости
category -> 1,2

And so on. That is, the news can have several categories and they are assigned by id.
Question:
How to correctly form an sql query to display news for the category selected in the select list?
For example, I chose to display only the "Chemistry" category in the list, and so on.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
galaxy, 2020-11-20
@galaxy

What subd? What is the category column type in site_news?
Your scheme is not normalized, on bare SQL it will not be possible to compose a query normally, most likely.

N
N, 2020-11-21
@Fernus

If the database is MySQL, then you can do this:

/* 1 - id категории "Химия" */
SELECT * FROM `site_news` WHERE FIND_IN_SET('1', `category`);

MySQL - FIND_IN_SET

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question