A
A
Alexey2015-10-23 20:06:43
MySQL
Alexey, 2015-10-23 20:06:43

How to pull an article in mysql?

For example, you need to pull out a lesson and tags for it. I do like this:

$res = $db->query('SELECT * FROM `news` WHERE `id` = 5');
$row = $res->fetch_assoc();
//выполняю с ним какие-то операции

// И дальше делаю еще один запрос, чтобы вытащить теги

$res = $db->query('SELECT * FROM `tags` WHERE `news_id` = 5');
$row = $res->fetch_assoc();

// И тут работаю с тегами

The question is whether this is correct, because if the lesson contains, in addition to tags, useful tips, a list of pictures, comments, progress steps, and users who have saved the lesson to their favorites, then in addition to these two requests, there will be a bunch of vague ones. Is it worth trying to somehow combine getting these results into one request, or am I paranoid?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kolya K, 2015-10-23
@Kolyagrozamorey

It seems to me that all this is combined into one table or, in extreme cases, into two. What for all that that concerns one news to carry on tables?

W
wol_fi, 2015-10-23
@wol_fi

Try JOIN.
For example like this:

SELECT * FROM `news` LEFT JOIN `tags`  
ON `tags`.`news_id` = `news`.`id` 
 WHERE `news`.`id` = 5

And by the same principle, you can type other tables.
This approach will reduce the number of requests, but this does not mean that it is better to do this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question